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.(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
的引用。
再加上當離開Activity後離開AsyncTask執行
if(!AsyncTask.isCancelled()){
AsyncTask.cancel(true);
}
我的應用程式是在onCreate時都會使用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //保持螢幕橫式
來保持螢幕橫式,但我發現在3.2版本的設備上或某些設備都還是會先轉直式再轉橫式,造成2次onCreate,所以我改成直接在AndroidManifest.xml的每個Activity加上android:screenOrientation="landscape"後似乎就不會有這個問題了
Great!
回覆刪除