开发者

Android - Reject null input problem

开发者 https://www.devze.com 2023-02-07 03:05 出处:网络
I am currently trying to get my code to reject a null (empty) input, every time I enter nothing for values it crashes, however telling it to reject \"0\" worked (as seen in the else if part of the cod

I am currently trying to get my code to reject a null (empty) input, every time I enter nothing for values it crashes, however telling it to reject "0" worked (as seen in the else if part of the code).

boolean check_input1 = Add.this.input.getText().toString().equals(null);
        boolean check_input2 = Add.this.input2.getText().toString().equals(null);
        boolean check_input3 = Add.this.input3.getText().toString().equals(null);

        float pricecheck_input2 = Float.valueOf(Add.this.input2.getText().toString().trim()).floatValue();
        float pricecheck_input3 = Float.valueOf(Add.this.input3.getText().toString().trim()).floatValue();

        if (check_input1 == true | check_input2 == true | check_input3 == true) {
       // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("No fields may be blank");

               alertbox.show();
        }
        else if (pricecheck_input2 == 0 | pricecheck_input3 == 0) {
         // prepare the alert box
               AlertDialog.Builder alertbox = new AlertDialog.Builder(Add.this);

               // set the message to display
               alertbox.setMessage("Prices must be greater than 0");

             开发者_运维百科  alertbox.show();
        }
        else {
         new InsertDataTask().execute(Add.this.input.getText().toString(), Float.toString(abv_ppl_calculation), Add.this.input3.getText().toString());
        }

Help would be greatly appreciated!


You need to examine the logic for your null check. You have:

boolean check_input1 = Add.this.input.getText().toString().equals(null);

The question is how would a toString() to result in null? Your code is on the right track since a little digging shows that .getText() will never return null (it's always a CharSequence). The right side of your check should instead check for the empty string. E.g.:

boolean check_input1 = Add.this.input.getText().toString().equals("");
0

精彩评论

暂无评论...
验证码 换一张
取 消