这个错误是什么,为什么会发生?

05-17 18:24:57.069: ERROR/WindowManager(18850): Activity com.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850): android.view.WindowLeaked: Activity ccom.mypkg.myP has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44c46ff0 that was originally added here
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.ViewRoot.<init>(ViewRoot.java:231)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.Dialog.show(Dialog.java:239)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.mypkg.myP$PreparePairingLinkageData.onPreExecute(viewP.java:183)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.AsyncTask.execute(AsyncTask.java:391)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.mypkg.myP.onCreate(viewP.java:94)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.os.Looper.loop(Looper.java:123)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at java.lang.reflect.Method.invoke(Method.java:521)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-17 18:24:57.069: ERROR/WindowManager(18850):     at dalvik.system.NativeStart.main(Native Method)

当前回答

唯一的答案是

  @Override
    public void onBackPressed() {

        exit_popup();
       // super.onBackPressed(); remove this line

    }

其他回答

也许你在activity中使用了findViewById而不是dialog。findViewById,然后在空实例上设置OnClickListener,这可能会导致原始错误。

尝试下面的代码,它将在任何时候工作,你将解散进度对话框,它将看到它的实例是否可用。

try {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

窗口泄漏异常有两个原因:

1)当Activity上下文不存在时显示对话框,为了解决这个问题,你应该只显示你确定Activity存在的对话框:

if(getActivity()!= null && !getActivity().isFinishing()){
        Dialog.show();
}

2)对话框不适当解散,解决使用以下代码:

@Override
public void onDestroy(){
    super.onDestroy();
    if ( Dialog!=null && Dialog.isShowing() ){
        Dialog.dismiss();
}
}

我正在使用一个视频播放器的对话框错误,而不是发疯(我已经测试了所有这些解决方案)

我选择了DialogFragment http://developer.android.com/reference/android/app/DialogFragment.html。

你可以在内部的DialogFragment类中返回构建器创建,只需要重写onCreateDialog

这个问题的答案都是正确的,但让我有点困惑,不知道为什么。在玩了大约2个小时后,我发现了这个错误的原因:

你已经知道,从阅读其他答案,有X已泄漏窗口DecorView@d9e6131[]错误意味着一个对话框打开时,你的应用程序关闭。但是为什么呢?

它可能是,当你的对话框打开时,你的应用程序因为其他原因崩溃了

这导致你的应用程序关闭,因为你的代码中的一些错误,这导致对话框保持打开的同时,你的应用程序关闭,由于其他错误。

所以,仔细检查你的逻辑。解决第一个错误,然后第二个错误会自己解决

一个错误导致另一个错误,又导致另一个错误,就像DOMINOS!