-1

java.lang.RuntimeException:无法启动活动 ComponentInfo { com.project/com.project.simple}:java.lang.NumberFormatException

EditText et1,et2,et3;
Button b1, b2;
Float two,three,four;   
@Override
protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.can);

et1 = (EditText) findViewById(R.id.editText1);

two = Float.valueOf(et1.getText().toString());

et2 = (EditText) findViewById(R.id.editText2);

three = Float.valueOf(et2.getText().toString());

et3 = (EditText) findViewById(R.id.editText3);

four = Float.valueOf(et3.getText().toString());

    b1 = (Button) findViewById(R.id.button1);

b2 = (Button) findViewById(R.id.button2);

b1.setOnClickListener(new OnClickListener() {
4

3 回答 3

0

It seems that the input is not valid. Please check twice that you don't try to parse a letters to a number. If you have a float please check that you use the right locale for parsing. E.g. in germany is pi 3,1415... and not 3.1415...

If you cannot preparse the values you could put the parsing trys in try catch blocks like this:

float value;
try {
    value=Float.parseFloat(someString);
} catch(NumberFormatException e) {
    // input was no valid float
}
于 2013-01-28T19:12:21.400 回答
0

您在onCreate()指定字段的位置。用户没有时间输入任何有效数据。您需要将数据的获取移到其他地方……就像您的那样onClick()

于 2013-01-28T19:15:11.113 回答
0

将您的代码从 onCerate 移动到其他方法(例如 Button 的 onClick 方法),您应该尝试以下操作:

try{
   two = Float.parseFloat(et1.getText().toString());
}catch(NumberFormatException e){
   two = 0;       
   Toast toast = Toast.makeText(this, 'Invalid number format on `two` field', 500);
   toast.show();  
} 

对于您要阅读的每个文本字段注释:浮点格式是 2.3,不要使用 2,3

于 2013-01-28T19:16:17.953 回答