当前位置:操作系统 > 安卓/Android >>

Android异常一、异步任务导致的窗口句柄泄漏问题

先贴异常:
[java]
05-05 10:36:41.009: E/WindowManager(4243): Activity com.tao.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405241b0 that was originally added here 
05-05 10:36:41.009: E/WindowManager(4243): android.view.WindowLeaked: Activity com.tao.MyActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405241b0 that was originally added here 
05-05 10:36:41.009: E/WindowManager(4243):     at android.view.ViewRoot.<init>(ViewRoot.java:265) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.view.Window$LocalWindowManager.addView(Window.java:424) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.Dialog.show(Dialog.java:241) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ProgressDialog.show(ProgressDialog.java:107) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ProgressDialog.show(ProgressDialog.java:95) 
05-05 10:36:41.009: E/WindowManager(4243):     at com.unifable.activity.MySecretaryActivity.onCreate(MySecretaryActivity.java:50) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.os.Handler.dispatchMessage(Handler.java:99) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.os.Looper.loop(Looper.java:130) 
05-05 10:36:41.009: E/WindowManager(4243):     at android.app.ActivityThread.main(ActivityThread.java:3683) 
05-05 10:36:41.009: E/WindowManager(4243):     at java.lang.reflect.Method.invokeNative(Native Method) 
05-05 10:36:41.009: E/WindowManager(4243):     at java.lang.reflect.Method.invoke(Method.java:507) 
05-05 10:36:41.009: E/WindowManager(4243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906) 
05-05 10:36:41.009: E/WindowManager(4243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:664) 
05-05 10:36:41.009: E/WindowManager(4243):     at dalvik.system.NativeStart.main(Native Method) 
 
这是在做一个项目的时候遇到的问题。在第一界面的Activity中,为了避免因程序假死而带来的糟糕用户体验,采用异步任务<span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px; ">AsyncTask</span>来访问网络,与服务器进行交互,并用<p><span style="white-space:pre"></span><span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px;">ProgressDialog控件来告知用户当前的程序进度。</span></p><p><span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px;">在网上查了下别人的资料,才终于明白原因。 
</span></p><p><span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px; ">上面的异常的意思是</span><span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">:</span>存在窗口句柄泄露,即未能及时销毁某个PhoneWindow。</p><p>为什么会出现这样的情况,因为,在异步任务里面已经发生了严重错误,而导致Activity的强制关闭。Activity强制关闭了,可是<span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px;">ProgressDialog</span>并没有dimiss()掉,所以出现了</p><p>窗口句柄的泄漏。而且,更严重的是,这个异常会覆盖我们在<span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px; ">AsyncTask里面发生的真正异常,</span>这往往误导了我们,把过多的精力放在查找所谓的内存泄露上了,而不是查找<span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px; ">AsyncTask</span> 
</p><p>中出现的情况。</p><p> 
</p><p><span style="white-space:pre"></span>参照别人的解决方法:</p><p><span style="white-space: pre; "></span>本解决方法并不能真正的解决问题,但是在一定程度上可以将真正导致错误的异常信息显露出来。即重写Activity的onDestroy方法,在方法中调用dismiss来解除对<span style="font-family:Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;border-collapse: collapse; line-height: 18px; ">ProgressDialog等的引用。</span></p>  



摘自 LonelyRoamer的专栏
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,