简体   繁体   English

onButtonClick中的“或逻辑”存在缺陷,导致强制关闭

[英]Flawed “or logic” in onButtonClick causing force close

I'm stuck with the last thing for my app. 我为我的应用程序所困扰。 When the button bSubmit is pressed, all the functions work except that app is force closing when no input is given in the EditText boxes, instead of force closing, I tried to put a toast msg, which doesn't work either. 当按下按钮bSubmit时,所有功能均起作用,除了在EditText框中未提供任何输入的情况下强制关闭应用程序,而不是强制关闭时,我尝试放置烤面包味精,该方法也不起作用。 I suppose there's a flaw in my logic 我想我的逻辑有缺陷

if (v.getId() == R.id.bSubmit) {

        String numstr = numberEditText.getText().toString();
        String pontstr = pointEditText.getText().toString();
        int point = Integer.parseInt(pontstr);
        //takes in the customer's number and the purchase amount

        //search for the customer's number in database
        DatabaseHelper helper = new DatabaseHelper(this);
        Log.d("onButtonClick",""+numstr);
        customer = helper.searchCustomer(numstr);



        if (numstr.equals(null) || pontstr.equals(null)){
            Context toastcontext = getApplicationContext();
            CharSequence text ="You haven't put any value";
            int duration = Toast.LENGTH_SHORT;


            Toast toast = Toast.makeText(toastcontext,text, duration);
            toast.show();

        }



        if (customer.getuNum() != null && customer.getuNum().equals(numstr)) {

            //if found then add the amount as new points
            //int row = helper.searchRow(customer.getuNum());
           // int point = helper.searchPoint(customer.getuNum());
            int pnt =  customer.getPoint() + point;
            Log.d("MainActivity",customer.getuNum()+" pnt "+pnt);
            helper.updatePoint(pnt,customer.getuNum());
            currentPoint.setText("Your point is "+pnt);
        }

        else {

            //create a new customer and add the info to database
            Customer customer = new Customer();
            customer.setuNum(numstr);
            customer.setPoint(point);
            helper.insertCustomer(customer);
            Log.d("MainActivity","customer null num "+customer.getuNum()+" point "+point);
            currentPoint.setText("Your point is " + point);
        }

        numberEditText.getText().clear();
        pointEditText.getText().clear();
    }

int point = Integer.parseInt(pontstr); will throw a NumberFormatException if the input is not an Integer (in the case of it being an empty String "" when nothing is in the EditText, or anything but an Integer value). 如果输入不是Integer(当EditText中没有内容或Integer值时,则为空字符串""时)将引发NumberFormatException This would cause the silent crash that you're talking about. 这将导致您正在谈论的无提示崩溃。

My advice would be to surround your parseInt() call with a try-catch block, and handle the case where the call would throw a NumberFormatException. 我的建议是用一个try-catch块包围您的parseInt()调用,并处理该调用将引发NumberFormatException的情况。 Something like this: 像这样:

int example; try { example = Integer.parseInt(pontstr); } catch (NumberFormatException e) { // Logic to deal with invalid input, maybe toast the user. }

  1. You will get number format exception if your string is not a number. 如果您的字符串不是数字,则会出现数字格式异常。 You need to put the statement after your empty checks are passed. 您需要在通过空支票后放入该语句。
  2. You are checking in wrong way . 您检查的方式有误。

    if (numstr.trim().isEmpty() || pontstr.trim().isEmpty(){ Context toastcontext = getApplicationContext(); CharSequence text ="You haven't put any value"; int duration = Toast.LENGTH_SHORT; 如果(numstr.trim()。isEmpty()|| pontstr.trim()。isEmpty(){上下文toastcontext = getApplicationContext(); CharSequence text =“您没有输入任何值”; int持续时间= Toast.LENGTH_SHORT;

      Toast toast = Toast.makeText(toastcontext,text, duration); toast.show(); } 

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM