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

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)

当前回答

我收到了同样模糊的错误信息,不知道为什么。根据前面的答案给出的线索,我将对mDialog.finish()的非gui调用更改为mDialog.dismiss(),错误消失了。这并没有影响我的小部件的行为,但它令人不安,很可能标志着一个重要的内存泄漏。

其他回答

你可以通过一个简单的/愚蠢的错误得到这个异常,通过(例如)在显示AlertDialog后不小心调用finish(),如果你错过了switch语句中的break call语句……

   @Override
   public void onClick(View v) {
    switch (v.getId()) {
        case R.id.new_button:
            openMyAlertDialog();
            break; <-- If you forget this the finish() method below 
                       will be called while the dialog is showing!
        case R.id.exit_button:
            finish();
            break;
        }
    }

finish()方法将关闭Activity,但AlertDialog仍然显示!

因此,当您专注地盯着代码,寻找糟糕的线程问题或复杂的编码等时,不要只见树不见林。有时它可能只是一些简单而愚蠢的事情,如缺少break语句。:)

当您尝试在活动有效完成后显示警报时,会出现“活动已泄漏最初添加的窗口…”错误。

AFAIK,你有两个选择:

重新考虑登录alert:在实际退出activity之前调用对话框上的dismiss()。 将对话框放在不同的线程中,并在该线程上运行它(与当前活动无关)。

比之前的答案晚得多,在2022年人们使用Kotlin时,当Kotlin协程试图从错误的(非main)协程上下文修改弹出窗口时,我们也得到了错误。

产生错误:

CoroutineScope(Dispatchers.IO).launch{
    someCodeThatNeedsOtherContext()
    someViewBinding.someAttribute = someValue // We get the error here
}

不产生错误:

CoroutineScope(Dispatchers.IO).launch{
    someCodeThatNeedsOtherContext()
    CoroutineScope(Dispatchers.Main).launch{
        someViewBinding.someAttribute = someValue // No error here
    }
}

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

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

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

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

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

只需确保您的活动不会由于代码中某处引发的异常而意外关闭。通常在异步任务中,当activity在doinBackground方法中面临强制关闭时,asynctask返回到onPostexecute方法。