本文主要是介绍Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个bug日志
FATAL EXCEPTION: main
03-25 14:24:07.724: E/AndroidRuntime(4135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.syyx.jingubang.ky/com.anguotech.android.activity.InitAndUpdateActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427f01a0 is not valid; is your activity running?
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2072)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2097)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread.access$600(ActivityThread.java:137)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.os.Looper.loop(Looper.java:137)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread.main(ActivityThread.java:4808)
03-25 14:24:07.724: E/AndroidRuntime(4135): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 14:24:07.724: E/AndroidRuntime(4135): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-25 14:24:07.724: E/AndroidRuntime(4135): at dalvik.system.NativeStart.main(Native Method)
03-25 14:24:07.724: E/AndroidRuntime(4135): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427f01a0 is not valid; is your activity running?
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.view.ViewRootImpl.setView(ViewRootImpl.java:591)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.view.Window$LocalWindowManager.addView(Window.java:547)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.Dialog.show(Dialog.java:277)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.anguotech.android.util.MyProgressBar.show(MyProgressBar.java:47)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.anguotech.android.util.MyProgressBar.show(MyProgressBar.java:54)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.anguotech.android.activity.InitAndUpdateActivity.upLoadBaseData(InitAndUpdateActivity.java:97)
03-25 14:24:07.724: E/AndroidRuntime(4135): at com.anguotech.android.activity.InitAndUpdateActivity.onCreate(InitAndUpdateActivity.java:45)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.Activity.performCreate(Activity.java:5010)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 14:24:07.724: E/AndroidRuntime(4135): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2036)
03-25 14:24:07.724: E/AndroidRuntime(4135): ... 11 more
出现这个问题的原因,是因为在代码中的dialog和progressBar没有及时销毁的缘故。这两个都是依赖于Activity界面的,当Activtiy被销毁了,它们也必须调用cancle放来来销毁。
解决办法: 在Activity生命周期的onDestroy方法中销毁。
@Overrideprotected void onDestroy() {super.onDestroy();// 当Activity销毁时,必须把使用的dialog或者progressBar也销毁if (progressBar != null) {progressBar.cancel();progressBar = null;}if(dialog!=null){dialog.canel();dialog = null;}}
这篇关于Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.B的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!