<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">今天调试程序时log中突然打印这样的错误,但是程序并没有crash,为了不放过一个错误,我决定调查一下。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">当时是离开一个activity,然后提示是否退出此界面,接下来就打印此错误:</p>
<span style="font-size:18px;">12-23 18:11:36.476: E/WindowManager(10917): android.view.WindowLeaked: Activity com.lh.sale.SaleActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{43b71108 V.E..... R.....ID 0,0-560,210} that was originally added here 12-23 18:11:36.476: E/WindowManager(10917): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:389) 12-23 18:11:36.476: E/WindowManager(10917): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:252) 12-23 18:11:36.476: E/WindowManager(10917): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 12-23 18:11:36.476: E/WindowManager(10917): at android.app.Dialog.show(Dialog.java:286) 12-23 18:11:36.476: E/WindowManager(10917): at com.lh.sale.SaleActivity.startProgressDialog(SaleActivity.java:350) 12-23 18:11:36.476: E/WindowManager(10917): at com.lh.sale.SaleActivity.ListViewfoodcate(SaleActivity.java:234) 12-23 18:11:36.476: E/WindowManager(10917): at com.lh.sale.SaleActivity.access$24(SaleActivity.java:232) 12-23 18:11:36.476: E/WindowManager(10917): at com.lh.sale.SaleActivity$4.onItemSelected(SaleActivity.java:495) 12-23 18:11:36.476: E/WindowManager(10917): at android.widget.AdapterView.fireOnSelected(AdapterView.java:893) 12-23 18:11:36.476: E/WindowManager(10917): at android.widget.AdapterView.access$200(AdapterView.java:48) 12-23 18:11:36.476: E/WindowManager(10917): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861) 12-23 18:11:36.476: E/WindowManager(10917): at android.os.Handler.handleCallback(Handler.java:733) 12-23 18:11:36.476: E/WindowManager(10917): at android.os.Handler.dispatchMessage(Handler.java:95) 12-23 18:11:36.476: E/WindowManager(10917): at android.os.Looper.loop(Looper.java:136) 12-23 18:11:36.476: E/WindowManager(10917): at android.app.ActivityThread.main(ActivityThread.java:5016) 12-23 18:11:36.476: E/WindowManager(10917): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 18:11:36.476: E/WindowManager(10917): at java.lang.reflect.Method.invoke(Method.java:515) 12-23 18:11:36.476: E/WindowManager(10917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 12-23 18:11:36.476: E/WindowManager(10917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 12-23 18:11:36.476: E/WindowManager(10917): at dalvik.system.NativeStart.main(Native Method) 12-23 18:11:37.898: E/AndroidRuntime(10917): FATAL EXCEPTION: main 12-23 18:11:37.898: E/AndroidRuntime(10917): Process: com.example.longhu, PID: 10917 12-23 18:11:37.898: E/AndroidRuntime(10917): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{43b71108 V.E..... R.....ID 0,0-560,210} not attached to window manager 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:377) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:306) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.app.Dialog.dismissDialog(Dialog.java:329) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.app.Dialog.dismiss(Dialog.java:312) 12-23 18:11:37.898: E/AndroidRuntime(10917): at com.lh.sale.SaleActivity.stopProgressDialog(SaleActivity.java:355) 12-23 18:11:37.898: E/AndroidRuntime(10917): at com.lh.sale.SaleActivity.access$4(SaleActivity.java:353) 12-23 18:11:37.898: E/AndroidRuntime(10917): at com.lh.sale.SaleActivity$1.handleMessage(SaleActivity.java:189) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.os.Handler.dispatchMessage(Handler.java:102) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.os.Looper.loop(Looper.java:136) 12-23 18:11:37.898: E/AndroidRuntime(10917): at android.app.ActivityThread.main(ActivityThread.java:5016) 12-23 18:11:37.898: E/AndroidRuntime(10917): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 18:11:37.898: E/AndroidRuntime(10917): at java.lang.reflect.Method.invoke(Method.java:515) 12-23 18:11:37.898: E/AndroidRuntime(10917): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 12-23 18:11:37.898: E/AndroidRuntime(10917): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 12-23 18:11:37.898: E/AndroidRuntime(10917): at dalvik.system.NativeStart.main(Native Method) 12-23 18:12:01.340: E/AudioManagerAndroid(11810): BLUETOOTH permission is missing! 12-23 18:12:01.379: E/libEGL(11810): call to OpenGL ES API with no current context (logged once per thread) 12-23 18:12:23.479: E/WindowManager(11810): android.view.WindowLeaked: Activity com.lh.sale.SaleActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{43906ba8 V.E..... R.....ID 0,0-560,210} that was originally added here 12-23 18:12:23.479: E/WindowManager(11810): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:389) 12-23 18:12:23.479: E/WindowManager(11810): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:252) 12-23 18:12:23.479: E/WindowManager(11810): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 12-23 18:12:23.479: E/WindowManager(11810): at android.app.Dialog.show(Dialog.java:286) 12-23 18:12:23.479: E/WindowManager(11810): at com.lh.sale.SaleActivity.startProgressDialog(SaleActivity.java:350) 12-23 18:12:23.479: E/WindowManager(11810): at com.lh.sale.SaleActivity.ListViewfoodcate(SaleActivity.java:234) 12-23 18:12:23.479: E/WindowManager(11810): at com.lh.sale.SaleActivity.access$24(SaleActivity.java:232) 12-23 18:12:23.479: E/WindowManager(11810): at com.lh.sale.SaleActivity$4.onItemSelected(SaleActivity.java:495) 12-23 18:12:23.479: E/WindowManager(11810): at android.widget.AdapterView.fireOnSelected(AdapterView.java:893) 12-23 18:12:23.479: E/WindowManager(11810): at android.widget.AdapterView.access$200(AdapterView.java:48) 12-23 18:12:23.479: E/WindowManager(11810): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861) 12-23 18:12:23.479: E/WindowManager(11810): at android.os.Handler.handleCallback(Handler.java:733) 12-23 18:12:23.479: E/WindowManager(11810): at android.os.Handler.dispatchMessage(Handler.java:95) 12-23 18:12:23.479: E/WindowManager(11810): at android.os.Looper.loop(Looper.java:136) 12-23 18:12:23.479: E/WindowManager(11810): at android.app.ActivityThread.main(ActivityThread.java:5016) 12-23 18:12:23.479: E/WindowManager(11810): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 18:12:23.479: E/WindowManager(11810): at java.lang.reflect.Method.invoke(Method.java:515) 12-23 18:12:23.479: E/WindowManager(11810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 12-23 18:12:23.479: E/WindowManager(11810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 12-23 18:12:23.479: E/WindowManager(11810): at dalvik.system.NativeStart.main(Native Method) 12-23 18:12:24.926: E/AndroidRuntime(11810): FATAL EXCEPTION: main 12-23 18:12:24.926: E/AndroidRuntime(11810): Process: com.example.longhu, PID: 11810 12-23 18:12:24.926: E/AndroidRuntime(11810): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{43906ba8 V.E..... R.....ID 0,0-560,210} not attached to window manager 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:377) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:306) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.app.Dialog.dismissDialog(Dialog.java:329) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.app.Dialog.dismiss(Dialog.java:312) 12-23 18:12:24.926: E/AndroidRuntime(11810): at com.lh.sale.SaleActivity.stopProgressDialog(SaleActivity.java:355) 12-23 18:12:24.926: E/AndroidRuntime(11810): at com.lh.sale.SaleActivity.access$4(SaleActivity.java:353) 12-23 18:12:24.926: E/AndroidRuntime(11810): at com.lh.sale.SaleActivity$1.handleMessage(SaleActivity.java:189) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.os.Handler.dispatchMessage(Handler.java:102) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.os.Looper.loop(Looper.java:136) 12-23 18:12:24.926: E/AndroidRuntime(11810): at android.app.ActivityThread.main(ActivityThread.java:5016) 12-23 18:12:24.926: E/AndroidRuntime(11810): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 18:12:24.926: E/AndroidRuntime(11810): at java.lang.reflect.Method.invoke(Method.java:515) 12-23 18:12:24.926: E/AndroidRuntime(11810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 12-23 18:12:24.926: E/AndroidRuntime(11810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 12-23 18:12:24.926: E/AndroidRuntime(11810): at dalvik.system.NativeStart.main(Native Method)</span>显然,我用Dialog确认是否退出,但是activity销毁后并没有处理Dialog的退出。
解决办法:
知道了问题所在就容易解决问题了。只需要在activity销毁时关闭Dialog就好。
<span style="font-size:18px;">@Override protected void onDestroy() { if(progressDialog != null) { progressDialog.dismiss(); } super.onDestroy(); } </span>