2014-10-26 74 views
2

我就一个要求,在那里我有两个文本字段,当我进入第一个文本字段摄氏温度值工作自动将其转换成华氏应用程序崩溃时,我曾经进入negitive值

我现在面临的问题是当过我进入负值应用程序崩溃

这里是我的代码

我的XML文件

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Celsius" /> 

<EditText 
    android:id="@+id/myTextBox" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberSigned" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Forenheight" /> 

<EditText 
    android:id="@+id/outputBox" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberSigned" /> 

<TextView 
    android:id="@+id/tv_in_for" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Forenheit" /> 

<EditText 
    android:id="@+id/et_in_for" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberSigned" /> 

<TextView 
    android:id="@+id/tv_out_cel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Celsius" /> 

<EditText 
    android:id="@+id/et_out_cel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberSigned" /> 

</LinearLayout> 

这是我的Java代码

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.type2); 
    final EditText outputTextBox = (EditText) findViewById(R.id.outputBox); 
    final EditText myTextBox = (EditText) findViewById(R.id.myTextBox); 
    myTextBox.addTextChangedListener(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) { 

      String inputs = myTextBox.getText().toString(); 
      if (TextUtils.isEmpty(inputs)) { 
       outputTextBox.setText("0"); 
      } else { 
       float i = Float.parseFloat(inputs); 
       i= ((i * 9)/5) + 32; 
       outputTextBox.setText(String.valueOf(i)); 
      } 
     } 
    }); 

    final EditText in_for = (EditText) findViewById(R.id.et_in_for); 
    final EditText out_cel = (EditText) findViewById(R.id.et_out_cel); 
    in_for.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      String inputs = in_for.getText().toString(); 
      if (TextUtils.isEmpty(inputs)) { 
       in_for.setText("0"); 
      } else { 
       float i = Float.parseFloat(inputs); 
       i= ((i - 32) * 5/9); 
       out_cel.setText(String.valueOf(i)); 
      } 
     } 
    }); 
} 

请帮我在这里。

+0

添加您的logcat – 2014-10-26 18:06:32

+0

你尽一切onTextChanged,没有检查,如果它是一个有效的数字。所以,当你输入“ - ”这不是一个有效的数字,你有一个崩溃。 – 2014-10-26 18:08:07

+0

抱歉说我的logcat不能正常工作。 – Irfan 2014-10-26 18:08:12

回答

0

这没有经过测试,但我假设你的问题是它试图解析-作为一个浮点数,这是不能做到的。为了解决这个问题,catch例外:

 //... 
     } else { 
      try { 
       float i = Float.parseFloat(inputs); 
       i= ((i * 9)/5) + 32; 
       outputTextBox.setText(String.valueOf(i)); 
      } catch (NumberFormatException e) { 
       outputBox.setText("0"); 
      } 
     } 
     //... 
+0

非常感谢你对我完美的工作。 :-) – Irfan 2014-10-26 18:14:52