2012-07-29 96 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))); 
       } 
相关问题