活动已经泄漏的窗口 - Android [英] Activity has leaked window - Android

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

问题描述

查看这些代码:



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



问题 / p>

当我点击主页按钮时,会抛出异常活动已经泄漏窗口...从这行:

  localWindowManager.addView(colourView,layoutParams); 

问题



您知道是什么原因导致的吗?



当我使用后退按钮关闭应用程序时不会发生问题。



异常/错误日志

  W / InputManagerService非聚焦客户端上的输入com.android.internal.view.IInputMethodClient$Stub$Proxy@40908148(uid = 10056 pid = 1368)
D / CordovaActivity(1368):CordovaActivity.onDestroy()
D / CordovaWebView(1368):>>>> loadUrlNow()
E / WindowManager(1368):活动com.phonegap.helloworld.HelloWorld已经泄露了窗口pl.edu.uj.tcs.student.xxx.Display$Layer@40589368,最初添加到这里
E / WindowManager(1368):android.view.WindowLeaked:Activity com.phonegap.helloworld.HelloWorld已经泄露窗口pl.edu.uj.tcs.student.xxx.Display$Layer@40589368这是最初添加到这里$ b $ < init>(ViewRoot.java:258)
E / WindowManager(1368):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java: (1368)
E / WindowManager(1368):在android.view.Window $ LocalWindowManager.addView(1368)$ window $($)
E / WindowManager(1368):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 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):在android.os.Handler.handleCallback(Handler.java:587 )
E / WindowManager(1368):在android.os.Looper.loop(Looper.java)上面的窗口管理器(1368):在android.os.Handler.dispatchMessage(Handler.java:92) :130)
E / WindowManager(1368):在android.app.ActivityThread.main(ActivityThread.java:3683)
E / WindowManager(1368):在java.lang.reflect.Method.invokeNative (NativeMethod)
E / WindowManager(1368):在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 onPageFinished(about:blank)
D / CordovaActivity(1368):onMessage(onPageFinished,about:blank)
D / CordovaActivity(1368):onMessage(exit,null)
I / power 96):*** set_screen_state 0

EDIT:

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



编辑2:



为什么会出现问题?因为我创建的是扩展CordovaPlugin和小辅助类的类。就这样。我不能(我想)修改Activity类的主体。我可以做的是通过调用 cordova.getActivity()函数。

解决方案

编程中的泄漏是什么?



您获取并不释放的内存会导致内存泄漏。类似的情况发生在(windows / dialogs)。



这里发生了什么?



你正在尝试添加一个窗口,当它显示它在前台,但当你按下主页按钮,它被暂停,然后被停止(尝试在onStop()和onPause()放烤面包。



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



解决方案



您的 onStop()或onPause() onDestroy() dismiss()如果是对话框)或删除它( remove() p>

在您的on unload函数中添加dismiss或remove函数,因为您提到在按回按钮时会收到此错误。退出应用程序时,它的 onUnload



因为我可以观察到你正在尝试做一个系统警报窗口,它来自它下面的任何东西。在活动中添加这种弹出窗口是危险的,因为它可能会导致泄漏问题。
你实际上可以通过服务添加这样的窗口,所以它比你的活动更多,并显示在设备上的任何地方(如果这是你需要的)。 / p>

查看此信息



更新2 - Cordova生命周期



覆盖您的CordovaPlugin类中的onUnload方法。我尝试查找,但文档提到onPause和onResume方法的存在。如果你在CordovaPlugin类中有onUnload,然后删除你在视图类runOnUiThread方法中创建的视图。


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:

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

EDIT:

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

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

解决方案

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

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

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.

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

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.

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

Check this out

Update 2-The Cordova lifecycle

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.

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

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