单击按钮时未显示Alertbox

时间:2019-03-18 05:42:36

标签: java android

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Add new product");
        builder.setView(subView);
        builder.create();

        builder.setPositiveButton("ADD PRODUCT", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                final String name = nameField.getText().toString();
                final int quantity = Integer.parseInt(quantityField.getText().toString());


                if(TextUtils.isEmpty(name) || quantity <= 0)
                {
                    Toast.makeText(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
                }
                else{
                    Product newProduct = new Product(name, quantity);
                    mDatabase.addProduct(newProduct);

                    //refresh the activity
                    finish();
                    startActivity(getIntent());
                }

            }
        });

这是我的代码,产品是数据库,其中有两个字段:namequantity。现在,当我将字段留空时,应用程序崩溃。

我什至尝试过:

if(nameFields.getText().tostring().isEmpty() ||quantityFields.getText.toString.isEmpty())
{
//Toast for error
}

如果我在警报构建器中输入单个字段,即当我仅输入name时,它将接受不应接受的值。相反,它必须弹出一个警报,提示您未输入所有必需的值。如何设置此警报?

这是我得到的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.inducesmile.androidsqliteexample, PID: 15729
                  java.lang.NumberFormatException: For input string: ""
                      at java.lang.Integer.parseInt(Integer.java:533)
                      at java.lang.Integer.parseInt(Integer.java:556)
                      at com.inducesmile.androidsqliteexample.MainActivity$2.onClick(MainActivity.java:80)
                      at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:163)
                      at android.app.ActivityThread.main(ActivityThread.java:6238)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

2 个答案:

答案 0 :(得分:0)

请将此行添加到代码底部

AlertDialog alert = builder.create();
alert.show();

答案 1 :(得分:0)

问题在这里:

final int quantity = Integer.parseInt(quantityField.getText().toString());

即使quantityField为空,您也不管做什么。在尝试将其解析为Integer之后,您仅在 后检查它是否为空。错误告诉您。

  

java.lang.NumberFormatException:对于输入字符串:“”

因此,也许将onClick函数重写为以下内容:

final String name = nameField.getText().toString();
final String quantityStr = quantityField.getText().toString();

if(TextUtils.isEmpty(name) || TextUtils.isEmpty(quantityStr))
{
     Toast.makeText(MainActivity.this, "Something went wrong. Check your input values", Toast.LENGTH_LONG).show();
}
else {
    final int quantity = Integer.parseInt(quantityStr);
    // TODO: here you can check if quantity <= 0 and handle that appropriately
    Product newProduct = new Product(name, quantity);
    mDatabase.addProduct(newProduct);

    //refresh the activity
    finish();
    startActivity(getIntent());
}

我更改了:

  • 首先创建一个字符串quantityStr
  • 如果为空则显示错误,就像您对name字段所做的一样
  • 解析后 确认不为空