Android – alertdialog button does not exist
                                        
                    •
                    Android                                    
                Suddenly (there is no change in the project code) I began to get an error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{<package>}: java.lang.IllegalArgumentException: Button does not exist
The error points to a method that has not been 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'm trying to copy and use this code in other projects - the results are the same, and it worked not long ago (the same target API, etc.). You know what I'm ignoring?
resolvent:
Do not hard code in setbutton (...). 1. Use the constant in dialoginterface class to specify which button:
DialogInterface.BUTTON_ NEGATIVE
DialogInterface.BUTTON_ POSITIVE
DialogInterface.BUTTON_ NEUTRAL
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        