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

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)

当前回答

如果您正在处理LiveData,当更新值而不是使用LiveData时。尝试做liveData.postValue(someValue)

其他回答

我在我的视频播放器应用程序中获取了这些日志。这些消息是在视频播放器关闭时抛出的。有趣的是,我曾经以随机的方式在几次运行中获得这些日志。此外,我的应用程序不涉及任何progressdialog。最后,我用下面的实现解决了这个问题。

@Override
protected void onPause()
{
    Log.v("MediaVideo", "onPause");
    super.onPause();
    this.mVideoView.pause();
    this.mVideoView.setVisibility(View.GONE);
}

@Override
protected void onDestroy()
{
    Log.v("MediaVideo", "onDestroy");
    super.onDestroy();
}

@Override
protected void onResume()
{
    Log.v("MediaVideo", "onResume");
    super.onResume();
    this.mVideoView.resume();
}

覆盖OnPause调用mVideoView.pause()和设置可见性为GONE。通过这种方式,我可以解决“活动已泄漏窗口”日志错误问题。

你可以通过一个简单的/愚蠢的错误得到这个异常,通过(例如)在显示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语句。:)

如果您正在使用AsyncTask,那么该日志消息可能具有欺骗性。如果你在你的日志中查找,你可能会发现另一个错误,可能是在你的AsyncTask的doInBackground()方法中,这是使你当前的Activity爆炸,因此一旦AsyncTask返回..接下来的你都知道了。其他一些用户已经在这里解释了:-)

这里有一个解决方案,当你想要解散AlertDialog,但不想在活动中保留对它的引用。

解决方案要求您拥有androidx。项目中的生命周期依赖关系(我相信在评论的时候,这是一个常见的需求)

这让你可以将对话框的解散委托给外部对象(观察者),你不需要再关心它了,因为当活动终止时它会自动取消订阅。(这里是证明:https://github.com/googlecodelabs/android-lifecycles/issues/5)。

因此,观察者保持对对话的引用,而活动保持对观察者的引用。当“onPause”发生时-观察者解散对话框,当“onDestroy”发生时-活动删除观察者,所以没有泄漏发生(好吧,至少我不会在logcat中看到错误)

// observer
class DialogDismissLifecycleObserver( private var dialog: AlertDialog? ) : LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause() {
        dialog?.dismiss()
        dialog = null
    }
}
// activity code
private fun showDialog() {
        if( isDestroyed || isFinishing ) return
        val dialog = AlertDialog
            .Builder(this, R.style.DialogTheme)
            // dialog setup skipped
            .create()
        lifecycle.addObserver( DialogDismissLifecycleObserver( dialog ) )
        dialog.show()
}

解决方案是在退出Activity之前在view .java:183中创建的对话框上调用dismiss(),例如在onPause()中。所有的窗口和对话框应该在离开一个活动之前关闭。