0

我有 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)
4

6 回答 6

1

请注意,您可以使用 setError("error");

于 2013-01-03T14:45:31.247 回答
1

此代码中有两个单独的问题:在空字符串上调用 parseInt 会引发异常(您看到的错误),并且您正在对 EditText 对象本身进行 .equals 比较,而不是对包含在里面的字符串. 请执行下列操作:

public void onClick(DialogInterface dialog, int id) {

   String itemString = itemET.getText().toString();
   String quantityString = quantityET.getText().toString();
   int quantity;

   if(itemString.equals(""))
   {
      Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
      t.show();   
   }

   if(quantityString.equals("")){
      Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
      t.show();
   }
   else {
      quantity = Integer.parseInt(quantityString);
   }
}

如果您提供的不是数字(例如,如果您在框中键入“a”),这仍然会崩溃,但它应该能够处理任何输入的内容。

于 2012-09-20T02:42:47.793 回答
0

在创建 EditText 时声明android:inputType="number"属性 init 只接受数值。

    public void onClick(DialogInterface dialog, int id) 
    {
                String item;
                int quantity;
                if ( itemET != null )
                {
                     item = itemET.getText().toString();
                }

                if ( quantityET != null && quantityET.getText().toString().trim().length() > 0 )
                {
                     quantity = Integer.parseInt(quantityET.getText().toString().trim());  // trim() method added when converting String to Integer
                }
        if(itemET.getText().trim().equals(""))  // Modification done here
        {
            Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
            t.show();   
        }
        if(quantityET.getText().trim().equals(""))  // Modification done here
        {
            Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
            t.show();
        }
    }
于 2012-09-20T02:03:52.267 回答
0

以上2个答案都不好,因为:

String item = itemET.getText().toString();

在这里你应该检查一下item,如果你愿意,但不是在这条线上是崩溃。

int quantity = Integer.parseInt(quantityET.getText().toString());

这是问题所在,因为quantityET有一个空字符串并且没有被检查。

有两种解决方案:一种是检查quantityET.getText()不为空且长度> 0,第二种是将 Try-catch 用于Integer.parseInt(value)

我会结合2:

String quantity = quantityET.getText().toString();
if(quantity != null && quantity.length() > 0){
   try{
     int iQuantity = Integer.parseInt(quantityET.getText().toString());
   }catch(NumberFormatException nfe){
      Log.e("your tag", e);
   }
}

希望能帮助到你。

于 2012-09-20T02:12:32.583 回答
0

尝试这样的事情:

public void onClick(DialogInterface dialog, int id) {
    int quantity;
    String item;
    if(itemET.getTExt.toString().trim()!="") {
        item = itemET.getText().toString();   
    } else {
        Toast t =Toast.makeText(GroceryList.this, "Please enter item", 5000);
        t.show();   
    }

    if(quantityET.getText().toString().trim()!="") {
        quantity = Integer.parseInt(quantityET.getText().toString().trim());
    } else {
        Toast t =Toast.makeText(GroceryList.this, "Please enter quantity or enter 0 if none", 5000);
        t.show();
}
于 2012-09-20T02:48:22.210 回答
0
        if(editText.getText().toString().length()==0)
        Toast.makeText(Registration.this, "Plz Enter Your name", Toast.LENGTH_LONG).show();

    else if (editText1.getText().toString().length() == 0)
        Toast.makeText(Registration.this, "plz enter mobile no.", Toast.LENGTH_LONG).show();
    else {
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(i);
        finish();
    }
于 2016-07-27T10:28:31.623 回答