Android 异常 Unable to add window -- token null is not for an application

版权声明:本文为博主原创文章,未经博主允许不得转载。原文链接http://blog.csdn.net/adayabetter?viewmode=contents https://blog.csdn.net/adayabetter/article/details/80482570

Android 异常 Unable to add window – token null is not for an application

异常信息如下:

05-28 10:14:56.882218 16278 16278 D  crash log: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:800)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:351)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.app.Dialog.show(Dialog.java:322)
05-28 10:14:56.882218 16278 16278 D  crash log:     at com.zxts.system.update.UpdateManagerApp$2.handleMessage(UpdateManagerApp.java:169)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.os.Handler.dispatchMessage(Handler.java:110)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.os.Looper.loop(Looper.java:203)
05-28 10:14:56.882218 16278 16278 D  crash log:     at android.app.ActivityThread.main(ActivityThread.java:6251)
05-28 10:14:56.882218 16278 16278 D  crash log:     at java.lang.reflect.Method.invoke(Native Method)
05-28 10:14:56.882218 16278 16278 D  crash log:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
05-28 10:14:56.882218 16278 16278 D  crash log:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

问题原因:
从异常提示来看,调用Dialog.show()方法,显示对话框的时候导致了该异常。而构造Dialog的地方
AlertDialog.Builder builder = new AlertDialog.Builder(Context context);
此处的context,需要用Activity,而不是Application的Context.


解决方案:
在构造Dialog时,使用Activity,不使用Application 的Context.

猜你喜欢

转载自blog.csdn.net/adayabetter/article/details/80482570