Question

I currently have an alert dialog with an editText. The idea is that a person will enter their pin and if the pin is correct with the one in the database do something, however if it is blank it crashes the program. So I need some help to prevent the user from leaving the EditText blank.

I have my current alert code:

AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
        alert.setTitle("Welcome!");
        alert.setMessage("Please select on of the following options:");
        // Set an EditText view to get user input
        final EditText input = new EditText(this);
        alert.setView(input);
        pindb = pin;
        statuses = status;
        lockerNumbers = lockerNumber;
        if (statuses != 0) {
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            alert.setMessage("Enter your pin");
            InputFilter[] FilterArray = new InputFilter[1];
            FilterArray[0] = new InputFilter.LengthFilter(4);
            input.setFilters(FilterArray);
            alert.setButton("Leave", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (pins != pindb) {
                        Toast.makeText(MainActivity.this, "wrong",
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "correct",
                                Toast.LENGTH_LONG).show();
                        ourDb.open();
                        ourDb.removePin(lockerNumbers, pins, 0);
                        ourDb.close();
                    }

                    loadListView();
                }
            });

            alert.setButton2("Access", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (pins != pindb) {
                        Toast.makeText(MainActivity.this, "wrong",
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "correct",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            alert.setMessage("Enter a new 4 digit pin");
            alert.setButton3("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (statuses != 1) {
                        ourDb.open();
                        ourDb.createPin(lockerNumbers, pins, 1);
                        ourDb.close();
                        loadListView();

                    }
                }
            });
        }
        alert.show();

    }

Any ideas?

Logcat of crash:

04-03 17:22:30.109: E/AndroidRuntime(32612): java.lang.NumberFormatException: Invalid int: ""
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.invalidInt(Integer.java:137)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.parseInt(Integer.java:358)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.parseInt(Integer.java:331)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.samsunglockercenter.MainActivity$2.onClick(MainActivity.java:171)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.os.Looper.loop(Looper.java:157)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.app.ActivityThread.main(ActivityThread.java:5872)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.reflect.Method.invoke(Method.java:515)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

Try this..

After getting the string value check with if(value.length() <= 0|| value.equals(""){ if that condition exists show the toast Please Enter Pin else do your operation

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String value = input.getText().toString().trim();

                if(value.length() <= 0|| value.equals(""){
                    Toast.makeText(MainActivity.this, "Please Enter Pin",
                            Toast.LENGTH_LONG).show();
                }else{
                // Do something with value!
                pins = Integer.parseInt(value);
                if (pins != pindb) {
                    Toast.makeText(MainActivity.this, "wrong",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "correct",
                            Toast.LENGTH_LONG).show();
                    ourDb.open();
                    ourDb.removePin(lockerNumbers, pins, 0);
                    ourDb.close();
                }

                loadListView();
               }
            }
        });

OTHER TIPS

After getting the value in pins check and see if that is 0. If it isn't then there is a pin, but if it is then do nothing/ask them to enter a pin.

pins = Integer.parseInt(value);
if(value.length() != 0 || !value.equals("") || value != null)
{
    if (pins != pindb) 
    {
        Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_LONG).show();
    } 
    else 
    {
        Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_LONG).show();
        ourDb.open();
        ourDb.removePin(lockerNumbers, pins, 0);
        ourDb.close();
    }
}
else                   
{
    //ask to enter pin 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top