无法创建警报对话框

时间:2013-04-16 12:01:40

标签: android

我正在覆盖后退按钮。我想在离开我的应用程序时向用户显示确认对话框。在创建对话框时出现以下错误

 Activity com.example.netlogger.ActivityLogin has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@49ffa7b8 that was originally added here
04-16 16:47:40.885: E/WindowManager(7757): android.view.WindowLeaked: Activity com.example.netlogger.ActivityLogin has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@49ffa7b8 that was originally added here
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.ViewRoot.<init>(ViewRoot.java:247)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-16 16:47:40.885: E/WindowManager(7757):  at android.app.Dialog.show(Dialog.java:241)
04-16 16:47:40.885: E/WindowManager(7757):  at com.example.netlogger.ActivityLogin.onBackPressed(ActivityLogin.java:130)
04-16 16:47:40.885: E/WindowManager(7757):  at android.app.Activity.onKeyUp(Activity.java:1888)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.KeyEvent.dispatch(KeyEvent.java:1063)
04-16 16:47:40.885: E/WindowManager(7757):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
04-16 16:47:40.885: E/WindowManager(7757):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1703)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2473)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2443)
04-16 16:47:40.885: E/WindowManager(7757):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
04-16 16:47:40.885: E/WindowManager(7757):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:47:40.885: E/WindowManager(7757):  at android.os.Looper.loop(Looper.java:123)
04-16 16:47:40.885: E/WindowManager(7757):  at android.app.ActivityThread.main(ActivityThread.java:4633)
04-16 16:47:40.885: E/WindowManager(7757):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:47:40.885: E/WindowManager(7757):  at java.lang.reflect.Method.invoke(Method.java:521)
04-16 16:47:40.885: E/WindowManager(7757):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-16 16:47:40.885: E/WindowManager(7757):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-16 16:47:40.885: E/WindowManager(7757):  at dalvik.system.NativeStart.main(Native Method)

我的代码如下

@Override
    public void onBackPressed() {
        super.onBackPressed();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        if (!prefs.getBoolean("ask_for_exit", false)) {
            exitAlertDialog = new Builder(ActivityLogin.this)
                    .setTitle("Are You Sure?")
                    .setMessage("Are you sure you want to exit")
                    .setPositiveButton("YES",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    finish();
                                }
                            })
                    .setNegativeButton("NO", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    exitAlertDialog.dismiss();
                                }
                    }).create();
            exitAlertDialog.show();

        }
    }

这个错误是什么意思?如果有任何线索请告诉我。谢谢!

3 个答案:

答案 0 :(得分:2)

如果你打电话

super.onBackPressed();

来自onBackPressed()方法,然后会发生默认后退按钮行为,并且您的活动将被销毁而不会清除(或显示)您的AlertDialog。你应该注释掉那一行。

如果您的SharedPreference要显示AlertDialog,那么您应该在finish()块中明确调用else

if (!prefs.getBoolean("ask_for_exit", false)){
  //show alert
}else{
   finish();
}

答案 1 :(得分:0)

从函数中删除super.onBackPressed();,当使用它时,Activity会自动调用onStop();

答案 2 :(得分:0)

问题出在super.onBackPressed();删除该行。

super.onBackPressed();

退出应用。您正试图在其后添加警报拨号。这就是问题所在。