PFort
PFort

Reputation: 485

AlertDialog.cancel() and AlertDialog.dismiss() crash app

If I have

private AlertDialog somePrompt;

and later I call

somePrompt.dismiss();

or

somePrompt.cancel();

this will crash the app if the dialog was not previously shown like somePrompt.show(). I need to make sure that this dialog is cancelled or dismissed, so how do I check if it is shown/visible?

Upvotes: 0

Views: 930

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Most likely your app is crashing because you aren't initializing your AlertDialog in these cases since you aren't calling show() on them. You probably only initialize them just before calling show(). You need to check if they arenull` before calling these closing methods on them.

if (somePrompt != null)
{
    somePrompt.dismiss();
}

If this doesn't help then please post your logcat from the crash.

Also, if you need to see if it is shown before calling it then you can add an isShowing check

if (somePrompt != null && somePrompt.isShowing())
{
    somePrompt.dismiss();
}

Upvotes: 1

Related Questions