从onOptionsItemSelected显示对话框

时间:2015-07-29 18:51:11

标签: java android

我正在尝试从菜单工具栏显示我的AlertDialog并使用此代码进行此异常:

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)

2 个答案:

答案 0 :(得分:2)

您忘记了案例陈述中的break;指令。 我认为这是你的问题。您还可以检查是否调用了finish();方法,因为此错误通常会告诉您对话框是从已关闭的活动中显示的

答案 1 :(得分:1)

答案是不要将getApplicationContext()传递给Dialog Builder