Kirill Zotov
Kirill Zotov

Reputation: 571

Show dialog from onOptionsItemSelected

I am trying to show my AlertDialog from menu Toolbar and have this exception with this code:

case R.id.about: {
    final AlertDialog.Builder aboutDialog = new AlertDialog.Builder(SplashActivity.this);
    aboutDialog.setTitle(mCtx.getResources()
        .getString(R.string.about_tests))
        .setCancelable(true)                           
        .setMessage(mCtx.getResources().getString(R.string.description_tests))                        
        .setPositiveButton(mCtx.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                googleRateClick();
            }
        })
        .setNegativeButton(mCtx.getResources().getString(R.string.not_now), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();
            }
        });

    aboutDialog.show();
}
1433-1433/MYPACKAGE E/WindowManager﹕ android.view.WindowLeaked: Activity MYPACKAGE.activity.SplashActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{d3dca6c V.E..... R......D 0,0-729,665} that was originally added here
                at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
                at android.app.Dialog.show(Dialog.java:298)
                at android.app.AlertDialog$Builder.show(AlertDialog.java:987)
                at MYPACKAGE.SplashActivity.onOptionsItemSelected(SplashActivity.java:90)

Upvotes: 0

Views: 362

Answers (2)

Kirill Zotov
Kirill Zotov

Reputation: 571

The answer is don't pass getApplicationContext() into Dialog Builder

Upvotes: 1

mariuss
mariuss

Reputation: 1247

You forgot the break; instruction in the case statement. I think that's your issue. You might as well check if you call finish(); method , because this error usually tells you that your dialog is showing from a closed activity

Upvotes: 2

Related Questions