android Dialog无法获取窗口问题闪退

java.lang.RuntimeException: Unable to start receiver com.zm.user.huowuyou.jpush.MyReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2c295e68 is not valid; is your activity running?
	at android.app.ActivityThread.handleReceiver(ActivityThread.java:2893)
	at android.app.ActivityThread.access$1800(ActivityThread.java:188)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
	at android.os.Handler.dispatchMessage(Handler.java:111)
	at android.os.Looper.loop(Looper.java:194)
	at android.app.ActivityThread.main(ActivityThread.java:5701)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@2c295e68 is not valid; is your activity running?
	at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
	at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:289)
	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:90)
	at android.app.Dialog.show(Dialog.java:311)


开发中遇到此问题  还是不定时的出现好扰人


解决办法:

在dialog的show时做下判断当前的宿主activity是否存在前台

if(!((Activity) context).isFinishing())
{
    //show dialog
}
希望对大家有帮助
android dialog无法获取窗口问题

你可能感兴趣的:(Android)