Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog Button does not exist

Suddenly (without any changes in this project code) I started to obtained an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: java.lang.IllegalArgumentException: Button does not exist

that error points to a method which wasn't yet called.

private void dialog(String title, String content){
    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(content);
    alertDialog.setCancelable(true);
    alertDialog.setButton(1, "OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               dialog.dismiss();
               }
            });
    alertDialog.show();
}

I tried to copy and use that code in other project - same result, and it was working not long ago (same target API etc.). Any idea what I'm overlooking?

like image 971
Michał Tajchert Avatar asked Dec 22 '13 01:12

Michał Tajchert


People also ask

How do I use AlertDialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is AlertDialog message?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.


1 Answers

Don't hardcode 1 in setButton(...). Use the constants found in the DialogInterface class to specify which button:

DialogInterface.BUTTON_NEGATIVE

DialogInterface.BUTTON_POSITIVE

DialogInterface.BUTTON_NEUTRAL

like image 112
Gautam Avatar answered Nov 01 '22 15:11

Gautam