在我的程序中,当我尝试计算而不给编辑文本框的数字值时程序崩溃。 如果我给出值,应用程序正常工作。当它崩溃我在log cat中得到以下错误
11-07 23:18:51.253: E/AndroidRuntime(450): FATAL EXCEPTION: main
11-07 23:18:51.253: E/AndroidRuntime(450): java.lang.NumberFormatException:
11-07 23:18:51.253: E/AndroidRuntime(450): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
11-07 23:18:51.253: E/AndroidRuntime(450): at java.lang.Double.parseDouble(Double.java:318)
11-07 23:18:51.253: E/AndroidRuntime(450): at com.xxx.xxx.Page1SubActivity$2.onClick(Page1SubActivity.java:77)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.view.View.performClick(View.java:2485)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.view.View$PerformClick.run(View.java:9080)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.os.Handler.handleCallback(Handler.java:587)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.os.Looper.loop(Looper.java:123)
11-07 23:18:51.253: E/AndroidRuntime(450): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-07 23:18:51.253: E/AndroidRuntime(450): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 23:18:51.253: E/AndroidRuntime(450): at java.lang.reflect.Method.invoke(Method.java:507)
11-07 23:18:51.253: E/AndroidRuntime(450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-07 23:18:51.253: E/AndroidRuntime(450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-07 23:18:56.723: I/Process(450): Sending signal. PID: 450 SIG: 9
这是Java代码。
ImageButton rightbuton = (ImageButton) findViewById(R.id.rightbutton);
rightbuton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
textview = (TextView) findViewById(R.id.textView5);
Double number2 = 0.0;
Double number3 = 0.0;
if (edit1.getText().toString().trim().length()>0 ) {
number2 = Double
.parseDouble(edit1.getText().toString());
}
if(edit2.getText().toString().trim().length()>0){
number3 = Double
.parseDouble(edit2.getText().toString());
}
number3 = number2 * number3;
int number2int = (int) (number2 * number3);
int number3int = (int) (number3 / 40);
number2 = number2 * number3;
number3 = number3 / 40;
if (number2int - number2 != 0) {
String number2val = Double.toString(number2);
textview.setText(number2val);
} else {
String number2val = Integer.toString(number2int);
textview.setText(number2val);
// label.text = [NSString stringWithFormat:@"%d",
// number2int];
}
if (number3int - number3 != 0) {
String number3val = Double.toString(number3);
textview.setText(number3val);
} else {
String number3val = Integer.toString(number3int);
textview.setText(number3val);
}
}
});
答案 0 :(得分:1)
在代码周围添加一个try / catch短语 -
try {
//all your code, starting from your first if statement and ending at the end of your method
catch (numberformatexception nfe){
//the code that you want to execute if there is not a number in the edittext
}
答案 1 :(得分:0)
谢谢你们,我只为布局中的数字设置了输入。因此,我猜javafile可以使用该信息。但是,我用try块
更正了代码Double number2 = 0.0;
Double number3 = 0.0;
try {
if (edit1.getText().toString().trim().length() > 0) {
number2 = Double.parseDouble(edit1.getText()
.toString());
}
if (edit2.getText().toString().trim().length() > 0) {
number3 = Double.parseDouble(edit2.getText()
.toString());
}
} catch (NumberFormatException e) {
Toast.makeText(getApplicationContext(), "bad input",
Toast.LENGTH_LONG).show();
}
感谢您帮助我纠正这个问题。