Android Activity 已经泄露了最初添加的窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2850573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:36:48  来源:igfitidea点击:

Activity has leaked window that was originally added

androidmemory-leaksdialog

提问by Pentium10

What is this error, and why does it happen?

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

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.access00(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)

回答by Alex Volovoy

You're trying to show a Dialog after you've exited an Activity.

您在退出活动后尝试显示对话框。

[EDIT]

[编辑]

This question is one of the top search on google for android developer, therefore Adding few important points from comments, which might be more helpful for future investigator without going in depth of comment conversation.

这个问题是谷歌Android开发者的热门搜索之一,因此从评论中添加一些重要的观点,这可能对未来的调查员更有帮助,而无需深入评论对话。

Answer 1:

答案 1

You're trying to show a Dialog after you've exited an Activity.

您在退出活动后尝试显示对话框。

Answer 2

答案 2

This error can be a little misleading in some circumstances (although the answer is still completely accurate) - i.e. in my case an unhandled Exception was thrown in an AsyncTask, which caused the Activity to shutdown, then an open progressdialog caused this Exception.. so the 'real' exception was a little earlier in the log

在某些情况下,这个错误可能有点误导(尽管答案仍然完全准确) - 即在我的情况下,在 AsyncTask 中抛出了一个未处理的异常,导致 Activity 关闭,然后打开的进度对话框导致了这个异常..所以“真正的”异常在日志中稍早一些

Answer 3

答案 3

Call dismiss() on the Dialog instance you created before exiting your Activity, e.g. in onPause() or onDestroy()

在退出 Activity 之前,例如在 onPause() 或 onDestroy() 中调用您创建的 Dialog 实例上的dismiss()

回答by molnarm

The solution is to call dismiss()on the Dialogyou created in viewP.java:183before exiting the Activity, e.g. in onPause(). All Windows&Dialogs should be closed before leaving an Activity.

解决的办法是呼叫dismiss()Dialog您创建中viewP.java:183退出之前Activity,在如onPause()。所有Windows& Dialogs 应该在离开之前关闭Activity

回答by ggomeze

If you are using AsyncTask, probably that log message can be deceptive. If you look up in your log, you might find another error, probably one in your doInBackground()method of your AsyncTask, that is making your current Activityto blow up, and thus once the AsyncTaskcomes back.. well, you know the rest. Some other users already explained that here :-)

如果您正在使用AsyncTask,则该日志消息可能具有欺骗性。如果你查看你的日志,你可能会发现另一个错误,可能是你的doInBackground()方法中的一个错误,AsyncTask它使你的电流Activity爆炸,因此一旦AsyncTask回来......好吧,你知道其余的。其他一些用户已经在这里解释过:-)

回答by Mark Phillip

I triggered this error by mistakenly calling hide()instead of dismiss()on an AlertDialog.

我被误调用触发此错误的hide(),而不是dismiss()AlertDialog

回答by Adrian Romanelli

You can get this exception by just a simple/dumb mistake, by (for example) accidentally calling finish()after having displayed an AlertDialog, if you miss a break call statement in a switch statement...

您可以通过一个简单/愚蠢的错误获得此异常,例如,如果您在 switch 语句中错过了 break 调用语句,则finish()在显示 之后意外调用AlertDialog...

   @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;
        }
    }

The finish()method will close the Activity, but the AlertDialogis still displaying!

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

So when you're staring intently at the code, looking for bad threading issues or complex coding and such, don't lose sight of the forest for the trees. Sometimes it can be just something as simple and dumb as a missing break statement. :)

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

回答by Ruchir Baronia

The answers to this question were all correct, but a little confusing for me to actually understand why. After playing around for around 2 hours the reason to this error (in my case) hit me:

这个问题的答案都是正确的,但让我真正理解为什么有点困惑。在玩了大约 2 个小时之后,这个错误的原因(就我而言)打击了我:

You already know, from reading other answers, that the has X has leaked window DecorView@d9e6131[]error means a dialog was open when your app closed. But why?

通过阅读其他答案,您已经知道有X has leaked window DecorView@d9e6131[]错误意味着在您的应用程序关闭时打开了一个对话框。但为什么?

It could be, that your app crashed for some other reason while your dialog was open

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

This lead to your app closing because of some bug in your code, which lead to the dialog remaining open at the same time as your app closed due to the other error.

这会导致您的应用程序因代码中的某些错误而关闭,这会导致对话框在您的应用程序因其他错误而关闭的同时保持打开状态。

So, look through your logical. Solve the first error, and then the second error will solve itselfenter image description here

所以,看看你的逻辑。解决第一个错误,然后第二个错误会自行解决在此处输入图片说明

One error causes another, which causes another, like DOMINOS!

一个错误会导致另一个错误,从而导致另一个错误,例如多米诺骨牌!

回答by Shoaib Ahmed

This problem arises when trying to show a Dialog after you've exited an Activity.

在退出活动后尝试显示对话框时会出现此问题。

I just solved this problem just by writing down the following code:

我只是通过写下以下代码解决了这个问题:

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

Basically, from which class you started progressDialog, override onDestroy method and do this way. It solved "Activity has leaked window" problem.

基本上,从哪个类开始progressDialog,覆盖onDestroy 方法并这样做。它解决了“活动已泄漏窗口”的问题。

回答by Tushar

I recently faced the same issue.

我最近遇到了同样的问题。

The reason behind this issue is that the activity being closed before the dialog is dismissed. There are various reasons for the above to happen. The ones mentioned in the posts above are also correct.

此问题背后的原因是在关闭对话框之前关闭了活动。出现上述情况有多种原因。上面帖子中提到的那些也是正确的。

I got into a situation, because in the thread, I was calling a function which was throwing exception. Because of which the window was being dismissed and hence the exception.

我陷入了一种情况,因为在线程中,我正在调用一个抛出异常的函数。因此,窗口被关闭,因此是例外。

回答by Muhammad Aamir Ali

Dismiss the dialog when activity destroy

活动销毁时关闭对话框

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

回答by sandy

This could help.

这可能会有所帮助。

if (! isFinishing()) {

    dialog.show();

    }