开发者

Error with alertdialog.dismiss()

I want to check a condition, then if it's false i want to dismiss an AlertDialog previously showen. However, i'm facing this error:

The method dismiss() is undefined for the type AlertDialog.Builder

Code:

ad.show();
        if (call.isInCall()== false)
        {
            ad.dismiss();
        }

What is the problem ?

Edit: prob开发者_开发知识库lem:

AlertDialog.Builder ad = new AlertDialog.Builder(context);
        d = ad.create();
        ad.setTitle("Appel en cours...");
        ad.setMessage("Voulez vous répondre à cet appel?");
        //ad.create();
        ad.setPositiveButton("Oui", 
.....

if(call.isInCall() == false && d != null && d.isShowing()){
           d.dismiss();
        }

=> Force Close. Thank you a lot for your help.


You will have to use the builder to create the dialog before you can do something like this.

//Let's change this so you have a field declared in your class.
AlertDialog d;

//Somewhere, maybe in onCreate() you're using the builder to instantiate the dialog.

//insert all builder creation and methods here first... then call
d = ad.create();

//somewhere else in your code you've shown the dialog with
d.show();

//again, some where else you're checking if the dialog is displaying and dismissing it
if(call.isInCall() == false && d != null && d.isShowing()){
   d.dismiss();
}

You'll have to be careful, of course, with your scope on the AlertDialog depending on where you are calling this code. This is also not really the recommended method for handling Dialogs. You should research the use of the onCreateDialog() onPrepareDialog() Activity callbacks: http://developer.android.com/guide/topics/ui/dialogs.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜