简体   繁体   中英

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. 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). 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. 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;

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

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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