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

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中,为了避免程序假死带来的糟糕的用户体验,采用异步任务AsyncTask来访问网络,与服务器进行交互,并用
ProgressDialog控件来告知用户当前的程序进度。

在网上查了下别人的资料。才终于明白了原因。
上面的异常的意思是:存在窗口句柄的泄漏,即未能及时的销毁某个PhoneWindow.

为什么会出现这样的情况,因为,在异步任务里面已经发生了严重的错误,而导致Activity的强制关闭。Activity强制关闭了,可是ProgressDialog并没有dismiss()掉,所以出现了
窗口句柄的泄漏。 而且,更严重的是,这个异常会覆盖我们在AsyncTask里面发生的真正异常,这往往会误导我们,把过多的精力放在查找所谓的内存泄漏的上面,而不是查找AsyncTask
中出现的错误。

参照别人的解决方法:
本解决方法并不能真正的解决问题,但是在一定程度上可以将真正导致错误的异常信息显露出来。即重写Activity的onDestory()方法,在该方法中调用dismiss()来解除对ProgressDialog
的引用。

  本文转自http://blog.csdn.net/lonelyroamer/article/details/7536962

posted @ 2013-07-09 15:39  fighter  阅读(8309)  评论(0编辑  收藏  举报