Activity 已泄露窗口 - Android [英] Activity has leaked window - Android

查看:29
本文介绍了Activity 已泄露窗口 - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这几段代码:

Android 上的自定义视图和窗口属性

问题

当我单击主页按钮"时,会抛出 exception:Activity has leaked window...来自这一行:

When I click 'Home button', exception is thrown: Activity has leaked window... from this line:

localWindowManager.addView(colourView, layoutParams);

问题

你知道是什么原因造成的吗?

Do you know what can cause it?

当我使用后退按钮关闭应用程序时不会出现问题.

Problem doesn't occur, when I close application with back button.

异常/错误日志

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$3.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

如何向 Cordova Activity 中的 onPause()、onStop() 等函数添加内容?

How can I add something to onPause(), onStop() etc. functions in Cordova Activity?

编辑 2:

为什么会出现这个问题?因为我创建的只是扩展 CordovaPlugin 的类和小的辅助类.就这样.我无法(我想)修改 Activity 类主体.我所能做的就是通过调用 cordova.getActivity() 函数来获取对它的引用.

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.

推荐答案

什么是编程漏洞?

您获取但未释放的内存会导致内存泄漏.(windows/dialogs)也会发生类似的情况.

The memory that you acquire and do not release lead to the memory leak.Similar happens with the (windows/dialogs).

这里发生了什么?

您正在尝试添加一个窗口,当它显示时它在前台,但是当您按下主页按钮时,它会暂停然后停止(尝试在 onStop() 和 onPause() 中添加祝酒词)).

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

由于您没有告诉系统删除​​您的视图,因此它仍然附加到现在已经从应用程序中消失/分离的窗口.因此根据系统,您的自定义视图占用了它没有释放的空间.

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.

解决方案

在你的 onStop() 或 onPause()onDestroy() 中,确保你关闭你的视图(dismiss() 如果它是一个对话框)或删除它(remove()如果使用窗口管理器添加).

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

在卸载函数中添加关闭或删除函数,正如您提到的,在按下后退按钮时会出现此错误.退出应用程序时,其 onUnload() 方法被调用.

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.

建议(如果不在上下文中,请忽略)

正如我所观察到的,您正在尝试创建一个系统警报窗口,该窗口会覆盖其下方的任何内容.在活动中添加此类弹出窗口是有风险的,因为它可能会导致泄漏问题.您实际上可以通过 Service 添加此类窗口,以便它比您的活动更持久并显示在设备上的任何地方(如果这是您需要的).

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

看看这个

更新 2 - Cordova 生命周期

为什么不尝试覆盖 CordovaPlugin 类中的 onUnload 方法.我尝试找到了 文档 提到了 onPause 和 onResume 方法的存在.如果您在 CordovaPlugin 类中有 onUnload,则删除您在视图类 runOnUiThread 方法中创建的视图.

Why don't you try to override onUnload method in your CordovaPlugin class.I tried finding but the docs are 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.

这篇关于Activity 已泄露窗口 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆