xain
xain

Reputation: 13839

Exception creating dialog inside a dialog in android

I have a couple of dialogs inside a dialog that are throwing the exception:

02-10 15:52:45.592: ERROR/AndroidRuntime(321): java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 2

The code is :

dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                ....

           showDialog(ID_DIALOG_SEND);

The dialog is defined as follows:

 @Override
 protected Dialog onCreateDialog(int id) {
        switch (id) {

    case ID_DIALOG_SEND:

        ProgressDialog loadingDialog = new ProgressDialog(this);
        loadingDialog.setMessage("Sending...");
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(false);
        return null;
      ....

And it doesn't work for this dialog either:

Builder b = new AlertDialog.Builder(this);

case ID_DIALOG_ERR:
b.setMessage("Error")
        .setCancelable(false)
        .setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        return null;

Any hints?

Thanks

Upvotes: 2

Views: 1845

Answers (2)

Hakan Ozbay
Hakan Ozbay

Reputation: 4719

Because you are trying to use showDialog(ID_DIALOG_SEND); in an Anonymous Inner class, it will give you a problem, as it thinks ShowDialog is a method of the inner class. You need to reference the outer Activity class, in which it is contained for it to work. So in your case i would do :

dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", 
 new DialogInterface.OnClickListener() {                 
    public void onClick(DialogInterface dialog, int which) {

           ....             

     thisActivityClassName.showDialog(ID_DIALOG_SEND);

or thisActivityClassName.this.showDialog(ID_DIALOG_SEND);

Otherwise if this fails, you might need to create a Handler to communicate with the outer class.

Also, i fully agree and reiterate dave.c's reply.

Upvotes: 1

dave.c
dave.c

Reputation: 10908

Isn't onCreateDialog supposed to return the Dialog that you create within the case statement, rather than returning null?

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {

case ID_DIALOG_SEND:

    ProgressDialog loadingDialog = new ProgressDialog(this);
    loadingDialog.setMessage("Sending...");
    loadingDialog.setIndeterminate(true);
    loadingDialog.setCancelable(false);
    return loadingDialog;
    ...

Upvotes: 3

Related Questions