我有 2 个 edittext 字段,它需要项目名称(字符串)和数量(整数)。如果用户在两个字段中均未输入任何内容,或者即使只有一个字段为空,我想显示一个祝酒词“请输入项目”或“请输入数量”。它创造和错误。这是下面的片段:
public void onClick(DialogInterface dialog, int id) {
String item = itemET.getText().toString();
int quantity = Integer.parseInt(quantityET.getText().toString());
if(itemET.equals("")){
Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
t.show();
}
if(quantityET.equals("")){
Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
t.show();
}
这是logcat中的错误:
09-19 10:01:51.766: E/AndroidRuntime(527): FATAL EXCEPTION: main
09-19 10:01:51.766: E/AndroidRuntime(527): java.lang.NumberFormatException: unable to parse '' as integer
09-19 10:01:51.766: E/AndroidRuntime(527): at java.lang.Integer.parseInt(Integer.java:362)
09-19 10:01:51.766: E/AndroidRuntime(527): at java.lang.Integer.parseInt(Integer.java:332)
09-19 10:01:51.766: E/AndroidRuntime(527): at com.mexican.recipes.GroceryList$1$1.onClick(GroceryList.java:60)
09-19 10:01:51.766: E/AndroidRuntime(527): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
09-19 10:01:51.766: E/AndroidRuntime(527): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 10:01:51.766: E/AndroidRuntime(527): at android.os.Looper.loop(Looper.java:123)
09-19 10:01:51.766: E/AndroidRuntime(527): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 10:01:51.766: E/AndroidRuntime(527): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 10:01:51.766: E/AndroidRuntime(527): at java.lang.reflect.Method.invoke(Method.java:507)
09-19 10:01:51.766: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 10:01:51.766: E/AndroidRuntime(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 10:01:51.766: E/AndroidRuntime(527): at dalvik.system.NativeStart.main(Native Method)