Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android NoSuchMethodException occurs at AlertDialog.Builder's setOnDismissListener

I am getting a NoSuchMethodException when using setOnDismissListener on Dialog in Android on a device with 4.1.2.

The same code is working on the emulator with version 4.2.2.

new AlertDialog.Builder(this)
   .setTitle(R.string.select_province)
   .setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface arg0) {
       //== other stuff
    }
}).show();

Any ideas?

like image 367
viv Avatar asked Jun 06 '13 19:06

viv


People also ask

What is the significance of AlertDialog builder?

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.

What are the methods of AlertDialog builder class?

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.

Which method is used to set a title to a alert dialog?

In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog.

How do I center the title of dialog alert in Android?

You cannot center the title in the default alert dialog. You will need to create a custom dialog in order to center the title.


1 Answers

A workaround to this issue is to just first create the dialog like this:

AlertDialog dialog = new AlertDialog.Builder(this).setTitle(R.string.select_province).create();

and then set the listener directly to the dialog:

dialog.setOnDismissListener( new OnDismissListener() {
    public void onDismiss(DialogInterface arg0) {
   //== other stuff
} );

then if you also want to show it:

dialog.show();

the result is the same and all these methods are supported since API 1.

AlertDialog.setOnDismissListener (DialogInterface.OnDismissListener listener)

AlerDialog.Builder.create()

like image 57
Mario Lenci Avatar answered Sep 27 '22 17:09

Mario Lenci