Null Exception在Android EditText中删除最后一个字符

时间:2012-07-29 12:56:01

标签: android android-edittext

我是Android编程的新手。 我的以下程序执行简单的Farenheit到Celsius转换。 如果您在Farenheit EditText中输入值,它会立即将其转换为摄氏度。  反之亦然。

我收到以下错误:

当我继续在Farenheit文本框中输入值时,没有问题。当我也删除该值时,它会删除最后一个字符。 如果我按退格键(在模拟器中)删除最后一个字符,它将停止运行。 我运行代码时出现以下错误。

2)即使焦点设置在Celsius EditText上,模拟器也始终在启动时将Fahreinheit显示为焦点

3)我在摄氏度编辑文本中所做的更改不会反映在华氏度中。

(请不要认为我在没有调试的情况下发布在论坛中,我已经尝试了3个多小时才发布这里,以便我可以保持这个论坛的清洁)

07-29 01: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 个答案:

答案 0 :(得分:3)

问题出在onTextChanged() - 方法:

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

if将永远不会成立,因为您检查的String-objects不是相同的。正确的方法是:

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

或者,甚至更好:

if (S.length > 0)

这会导致使用空字符串调用Float.parseFloat(String) - 方法,该字符串会抛出NumberFormatException

答案 1 :(得分:0)

您的代码正在尝试将NULL值转换为摄氏度,

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

答案 2 :(得分:0)

尝试检查字符串的长度:

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