2013-01-18 108 views
0

我一直在嘗試爲Android開發學習Java,因此我決定嘗試製作一個簡單的轉換器應用程序以供學習。目前我有一個簡單的用戶界面,我正試圖從攝氏轉換爲華氏。轉換器在工作時將轉換攝氏溫度,華氏溫度和開爾文溫度。學習Android Java - 應用程序失敗

當我點擊應該運行計算方法的按鈕時,出現錯誤「不幸的是,轉換器已停止。」 下面是我的代碼,我也包含了視圖的XML。

 

    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代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/app_name" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="numberDecimal" > 

     <requestFocus /> 
    </EditText> 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/temp_arr" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/temp_arr" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/convertBtn" 
     android:onClick="tempCalc" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

是任何人都能夠指出我要去的地方錯了,我不知道。由於

+6

那麼,你發佈除了異常堆棧之外的所有東西,這是理解問題的關鍵。請從logcat發佈堆棧跟蹤。 – kosa

回答

7

變化:

public void tempCalc() 

public void tempCalc(View v) 

任何的onClick方法需要View參數。由於您不通過,方法簽名不匹配,您的應用程序會引發異常。