Learning Android Java - application fails
我一直在努力学习Java for Android开发,所以我决定尝试制作一个简单的转换器应用程序来学习。 目前我有一个简单的用户界面,我正在尝试从摄氏温度转换为华氏温度。 转换器在工作时会转换为Celsius,Fahrenheit和Kelvin。
当我单击应该运行计算方法的按钮时,我收到错误"不幸的是,转换器已停止"。
下面是我的代码,我也为视图包含了XML。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | package com.michaelmurphy.converter; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.widget.Spinner; public class Temperature extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.temperature_view); // TODO Auto-generated method stub } public void tempCalc() { //define variables float value = 0; String from ="";//for spinner String to ="";//for spinner //get entered value EditText input = (EditText) findViewById(R.id.editText1); //convert to string String enteredValue = input.getText().toString(); //convert string into float float num = Float.valueOf(enteredValue); //retrieve the from spinner value final Spinner fromSpinner = (Spinner) findViewById(R.id.spinner1); from = fromSpinner.getSelectedItem().toString(); //retrieve the to spinner value final Spinner toSpinner = (Spinner) findViewById(R.id.spinner1); to = toSpinner.getSelectedItem().toString(); EditText output = (EditText) findViewById(R.id.textView2); /*if(from.equals(to)) //the same conversion type { //error }*/ if(from.equals("Celcius")) { if(to.equals("Fahrenheit")) { value = celToFar(num); } else { //value = celToKel(num); } } else if(from.equals("Fahrenheit")) { if(to.equals("Celcius")) { //value = fahToCel(num); } else { //value = fahToKel(num); } } else //kelvin { if(to.equals("Celcius")) { //value = kelToCel(num); } else { //value = kelToFah(num); } } //set the label to variable value String valueStr = Float.toString(value);//cast float to string output.setText(valueStr); } public float celToFar(float cel) { float fah = cel * 9/5 + 32; return fah; } } |
查看XML代码:
有人能够指出我哪里出错了,我不知道。 谢谢
更改:
1 | public void tempCalc() |
至
1 |
任何onClick方法都需要