Android異常一、非同步任務導致的視窗控制代碼泄漏問題

來源:互聯網
上載者:User

先貼異常:

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 here05-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 here05-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中,為了避免程式假死帶來的糟糕的使用者體驗,採用非同步任務AsyncTask來訪問網路,與伺服器進行互動,並用ProgressDialog控制項來告知使用者當前的程式進度。在網上查了下別人的資料。才終於明白了原因。上面的異常的意思是:存在視窗控制代碼的泄漏,即未能及時的銷毀某個PhoneWindow.為什麼會出現這樣的情況,因為,在非同步任務裡面已經發生了嚴重的錯誤,而導致Activity的強制關閉。Activity強制關閉了,可是ProgressDialog並沒有dismiss()掉,所以出現了視窗控制代碼的泄漏。 而且,更嚴重的是,這個異常會覆蓋我們在AsyncTask裡面發生的真正異常,這往往會誤導我們,把過多的精力放在尋找所謂的記憶體流失的上面,而不是尋找AsyncTask中出現的錯誤。參照別人的解決方案:本解決方案並不能真正的解決問題,但是在一定程度上可以將真正導致錯誤的異常資訊顯露出來。即重寫Activity的onDestory()方法,在該方法中調用dismiss()來解除對ProgressDialog的引用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.