Java Activity 已泄露窗口 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21817888/
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
Activity has leaked window - Android
提问by tomwesolowski
Look at these pieces of code:
看看这几段代码:
Custom views and window attributes on Android
Problem
问题
When I click 'Home button', exception
is thrown: Activity
has leaked window... from this line:
当我单击“主页按钮”时,exception
抛出:Activity
已泄漏窗口...来自这一行:
localWindowManager.addView(colourView, layoutParams);
Question(s)
问题)
Do you know what can cause it?
你知道是什么原因造成的吗?
Problem doesn't occur, when I close application with back button.
当我用后退按钮关闭应用程序时,问题不会发生。
Exception/Error Logs
异常/错误日志
W/InputManagerService( 96): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@40908148 (uid=10056 pid=1368)
D/CordovaActivity( 1368): CordovaActivity.onDestroy()
D/CordovaWebView( 1368): >>> loadUrlNow()
E/WindowManager( 1368): Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368): android.view.WindowLeaked: Activity com.phonegap.helloworld.HelloWorld has leaked window pl.edu.uj.tcs.student.xxx.Display$Layer@40589368 that was originally added here
E/WindowManager( 1368): at android.view.ViewRoot.<init>(ViewRoot.java:258)
E/WindowManager( 1368): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 1368): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 1368): at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 1368): at pl.edu.uj.tcs.student.xxx.Display.setColorsViews(Display.java:181)
E/WindowManager( 1368): at pl.edu.uj.tcs.student.xxx.Display.run(Display.java:139)
E/WindowManager( 1368): at android.os.Handler.handleCallback(Handler.java:587)
E/WindowManager( 1368): at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager( 1368): at android.os.Looper.loop(Looper.java:130)
E/WindowManager( 1368): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/WindowManager( 1368): at java.lang.reflect.Method.invokeNative(NativeMethod)
E/WindowManager( 1368): at java.lang.reflect.Method.invoke(Method.java:507)
E/WindowManager( 1368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/WindowManager( 1368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/WindowManager( 1368): at dalvik.system.NativeStart.main(Native Method)
D/CordovaActivity( 1368): onMessage(onPageStarted,about:blank)
D/CordovaWebViewClient( 1368): onPageFinished(about:blank)
D/CordovaActivity( 1368): onMessage(onPageFinished,about:blank)
D/CordovaActivity( 1368): onMessage(exit,null)
I/power ( 96): *** set_screen_state 0
EDIT:
编辑:
How can I add something to onPause(), onStop() etc. functions in CordovaActivity?
如何在CordovaActivity 中向 onPause()、onStop() 等函数添加内容?
EDIT 2:
编辑2:
Why is that a problem? Because all I create is class that extends CordovaPlugin and small auxilary classes. That's all. I'm not able (I suppose) to modify Activity class body. All I can do is get reference to it by calling cordova.getActivity()
function.
为什么这是一个问题?因为我创建的只是扩展 CordovaPlugin 的类和小的辅助类。就这样。我无法(我想)修改 Activity 类主体。我所能做的就是通过调用cordova.getActivity()
函数来引用它。
采纳答案by cafebabe1991
What is a leak in programming?
什么是编程中的泄漏?
The memory that you acquire and do not release lead to the memory leak.Similar happens with the (windows/dialogs).
您获得但未释放的内存导致内存泄漏。(windows/dialogs)也会发生类似的情况。
What's happening here?
这里发生了什么事?
You are trying to add a window and while it shows up it is on the foreground,but when you are pressing the home button it gets paused and then gets stopped (Try to put a toast in onStop() and onPause()).
您正在尝试添加一个窗口,当它显示时它位于前台,但是当您按下主页按钮时,它会暂停然后停止(尝试在 onStop() 和 onPause() 中放一个祝酒词)。
Since you did not tell the system to remove your view , hence it remains attached to the window that now has disappeared/detached from the application. Hence according to the system your customView occupied the space which it did not release.
由于您没有告诉系统删除您的视图,因此它仍然附加到现在已经从应用程序中消失/分离的窗口。因此,根据系统,您的 customView 占用了它没有释放的空间。
Solution
解决方案
Inside your onStop() or onPause()
andonDestroy()
make sure you dismiss your view(dismiss()
if it's a dialog) or remove it(remove()
if added using window Manager).
在您的内部onStop() or onPause()
并onDestroy()
确保您关闭您的视图(dismiss()
如果它是一个对话框)或删除它(remove()
如果使用窗口管理器添加)。
Add the dismiss or remove functions inside your on unload function as you mentioned that on pressing back button you get this error.On exiting an app its onUnload()
method gets called.
在卸载功能中添加关闭或删除功能,正如您提到的,在按下后退按钮时,您会收到此错误。退出应用程序时,它的onUnload()
方法会被调用。
Suggestion(Ignore if not in context)
建议(如果不在上下文中,请忽略)
As i can observe you are trying to make a System alert window that comes over anything beneath it.Adding such kind of pop ups in the activity is risky as it may cause leakage problems.
You may actually add such kind of window via a Service
so it outlives your activity and shows up everywhere on the device (if that is what you need).
正如我所观察到的,您正在尝试创建一个系统警报窗口,该窗口会覆盖其下方的任何内容。在活动中添加此类弹出窗口是有风险的,因为它可能会导致泄漏问题。您实际上可以通过 a 添加此类窗口,Service
以便它比您的活动更持久并显示在设备上的任何地方(如果这是您需要的)。
Update 2-The Cordova lifecycle
更新 2-Cordova 生命周期
Why don't you try to override onUnload method in your CordovaPlugin class.I tried finding but the docsare mentioning the existence of onPause and onResume methods.If you got onUnload in the CordovaPlugin class then remove the view you are making in your view class runOnUiThread method.
你为什么不尝试在你的 CordovaPlugin 类中覆盖 onUnload 方法。我试图找到但文档提到了 onPause 和 onResume 方法的存在。如果你在 CordovaPlugin 类中有 onUnload 然后删除你在视图类中创建的视图runOnUiThread 方法。
回答by Jebasuthan
You're trying to show a Dialog
after you've exited an Activity.
您试图Dialog
在退出活动后显示。
The solution is to call dismiss()
on the Dialog
you created before exiting the Activity
, e.g. in onPause(). All windows&dialogs should be closed before leaving an Activity
.
解决的办法是呼叫dismiss()
在Dialog
你离开之前创建的Activity
,例如在onPause()。在离开之前,应关闭所有窗口和对话框Activity
。
@Override
protected void onStop() {
super.onStop();
if (loadingDlg != null) {
loadingDlg.dismiss();
loadingDlg = null;
}
}
Hope it should helpful for you.
希望对你有帮助。
回答by Rick Royd Aban
Leaked window usually happens when your context show dialogs and that context is suddenly forced close that your dialog has not been dismissed properly.
当您的上下文显示对话框并且该上下文突然被强制关闭而您的对话框没有被正确关闭时,通常会发生泄漏的窗口。
In order to fix this, you have to fix your errors before the window has leaked error.
为了解决这个问题,您必须在窗口泄漏错误之前修复您的错误。
回答by Karthick pop
Check weather the dialog is showing or not
检查天气对话框是否显示
@Override
protected void onStop() {
super.onStop();
if (loadingDlg != null) {
if(loadingDlg.isShowing())
loadingDlg.dismiss();
loadingDlg = null;
}
}
回答by JanB
I had a finish() that was called place before my AlertDialog was called which caused the Activity to finish before the AlertDialog was called. I removed the finish() and put it after the user had completed their input using the AlertDialog and the window leak was fixed.
我有一个 finish() 在我的 AlertDialog 被调用之前被称为 place ,这导致 Activity 在 AlertDialog 被调用之前完成。我删除了 finish() 并在用户使用 AlertDialog 完成输入后放置它,并且修复了窗口泄漏。