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的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。