2012-07-29 43 views
0

我是Android編程的新手。 我的下面的程序做了一個簡單的華氏轉換爲攝氏溫度。 如果您在Farenheit EditText中輸入值,它會立即將其轉換爲攝氏溫度。 反之亦然。Null異常在Android EditText中刪除最後一個字符

,我發現了以下錯誤:

當我請華氏度文本框中輸入值,也沒有問題。當我也刪除這個值時,直到最後一個字符才刪除。 如果我按退格鍵(在模擬器中)刪除最後一個字符,它會停止運行。 當我運行我的代碼時,出現以下錯誤。

2)雖然重點是攝氏上設置的EditText模擬器始終顯示Fahreinheit當它開始

3)我做在攝氏編輯文本的變化不會反映在華氏作爲重點。

(請不要認爲我在論壇上張貼無需調試,我已經嘗試了3個多小時在這裏發帖之前,這樣我可以保持這個論壇乾淨)

1月7日至29日:59:21.189: E/AndroidRuntime(1390):java.lang.NumberFormatException:無效浮動: 「」

以下是我MainActivity.Java

public class MainActivity extends Activity { 
    private EditText celsiusText; 
    private EditText farenheitText; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     celsiusText = (EditText) findViewById(R.id.editText1); 
     farenheitText = (EditText) findViewById(R.id.editText2); 
     celsiusText.requestFocus(); 
     celsiusText.setOnFocusChangeListener((OnFocusChangeListener) this); 
     farenheitText.setOnFocusChangeListener((OnFocusChangeListener) this); 
    } 
     public void onFocusChange(View v, boolean hasFocus) 

     { 

      TextWatcher watcher1 = new TextWatcher(){ 
      public void afterTextChanged(Editable s) { 

       } 

       public void beforeTextChanged(CharSequence s, int start, 
       int count, int after) { 
       } 
      public void onTextChanged(CharSequence S,int start,int before, int count){ 
       float inputValue; 
       if (!S.toString().equals("")) 
       { 
        inputValue = Float.parseFloat(S.toString()); 
          ((EditText)findViewById(R.id.editText1)).setText(String 
            .valueOf(convertFahrenheitToCelsius(inputValue))); 

       } 
       else 
       { 
        ((EditText)findViewById(R.id.editText1)).setText(""); 
        return; 
       } 
         } 
      }; 



      TextWatcher watcher2 = new TextWatcher() 
      { 
         public void afterTextChanged(Editable s) { 

        } 

        public void beforeTextChanged(CharSequence s, int start, 
        int count, int after) { 
        } 
       public void onTextChanged(CharSequence S,int start,int before, int count){ 
        float inputValue; 
        if (!S.toString().equals("")) 
        { 
         inputValue = Float.parseFloat(S.toString()); 
        ((EditText)findViewById(R.id.editText2)).setText(String 
           .valueOf(convertCelsiusToFahrenheit(inputValue))); 

        } 
        else 
        { 
         ((EditText)findViewById(R.id.editText2)).setText(""); 
         return; 
        } 
          } 
       }; 

       if((v == findViewById(R.id.editText2)) && (hasFocus==true)) 
       { 
       farenheitText.addTextChangedListener(watcher1); 
       } 
       else if ((v == findViewById(R.id.editText1)) && (hasFocus==true)) 
       { 
        ((EditText)findViewById(R.id.editText1)).setText(""); 
       celsiusText.addTextChangedListener(watcher2); 
       } 
     } 





//Converts to celsius 
     private float convertFahrenheitToCelsius(float fahrenheit) { 
     return ((fahrenheit - 32) * 5/9); 
     } 

     // Converts to fahrenheit 
     private float convertCelsiusToFahrenheit(float celsius) { 
     return ((celsius * 9)/5) + 32; 
     } 

} 

我activity_main.xml中是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="62dp" 
     android:ems="10" 
     android:inputType="numberSigned" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
     android:inputType="numberSigned" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="18dp" 
     android:text="@string/celsius" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignRight="@+id/editText2" 
     android:text="@string/fahrenheit" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

正如你可以看到我的下一步將是編輯文本框雙方,如何將此代碼轉換爲攝氏(使用setfocus,以便它不會拋出錯誤)的任何幫助也將不勝感激。

回答

3

的問題是在onTextChanged() - 方法:

float inputValue; 
if (S != "") { 
    inputValue = Float.parseFloat(S.toString()); 
    // ... 
} 

的,如果永遠不會爲真,因爲字符串 - 您檢查的對象不是相同。正確的方法是:

if (!S.toString().equals("")) 

,或者甚至更好:

if (S.length > 0) 

這將導致Float.parseFloat(String) - 方法與一個空字符串,這方面拋出NumberFormatException被調用。

+0

非常感謝盧卡斯。 「if(!S.toString()。equals(」「))」訣竅。 – Mahesh 2012-07-29 14:00:30

+0

使用長度比使用「equals」更快。 – 2012-07-29 14:39:30

+0

盧卡斯感謝您的回答,我已經更新了我的問題,請你檢查我正在做的錯誤是什麼。 – Mahesh 2012-07-30 09:23:16

0

你的代碼試圖NULL值轉換成攝氏度,

if(inputValue==null){ 
// Handle the situation here 
} 
0

嘗試檢查字符串也長度:

if (S != "" && S.length() > 0){ 
        inputValue = Float.parseFloat(S.toString()); 
        celsiusText.setText(String 
          .valueOf(convertFahrenheitToCelsius(inputValue))); 
       } 
相關問題