当edittext字段(数字输入)留空时,我遇到了一些错误处理问题。所以我想要的是将edittext的值存储到字符串中,如果值不是“”,我将转换为整数并执行一些操作。但是,即使在转换为整数行之前,应用程序也会停止。有什么建议?这是我的代码:
bConfirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//number input edit field
etInputValue = etInput.getText().toString();
if (etInputValue.equals("")){
AlertDialog.Builder inpte = new AlertDialog.Builder(ManualInputActivity.this);
inpte.setTitle("Error");
inpte
.setMessage("Please Fill in all Fields")
.setCancelable(false)
.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id1) {
// TODO Auto-generated method stub
dialog.cancel();
Intent i4 = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i4);
}
});
AlertDialog inptDia = inpte.create();
inptDia.show();
}
else{//Do some calculations}
logcat的:
10-21 21:18:00.725: E/AndroidRuntime(1025): FATAL EXCEPTION: main
10-21 21:18:00.725: E/AndroidRuntime(1025): java.lang.NumberFormatException: Invalid int: ""
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.Integer.invalidInt(Integer.java:138)
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.Integer.parseInt(Integer.java:359)
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.Integer.parseInt(Integer.java:332)
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.Integer.valueOf(Integer.java:490)
10-21 21:18:00.725: E/AndroidRuntime(1025): at com.diatrack.app.ManualInputActivity$2.onClick(ManualInputActivity.java:97)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.view.View.performClick(View.java:4084)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.view.View$PerformClick.run(View.java:16966)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.os.Handler.handleCallback(Handler.java:615)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.os.Looper.loop(Looper.java:137)
10-21 21:18:00.725: E/AndroidRuntime(1025): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 21:18:00.725: E/AndroidRuntime(1025): at java.lang.reflect.Method.invoke(Method.java:511)
10-21 21:18:00.725: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-21 21:18:00.725: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-21 21:18:00.725: E/AndroidRuntime(1025): at dalvik.system.NativeStart.main(Native Method)