java.lang.RuntimeException: Unable to start receiver com.yeliner.example.fragmentnews.receiver.Offl

问题:

 java.lang.RuntimeException: Unable to start receiver com.yeliner.example.fragmentnews.receiver.OfflineReceiver:
 ......
    Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

准备在onReceive方法中,弹出一个对话框,结果报以上错误,后来添加了一行代码搞定!
在alertDialog.show()前添加:

 //设置类型
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();

原因:由于是在广播接收器中启动的,所以要把对话框的类型设为:TYPE_SYSTEM_ALERT,才会从广播接收器中弹出
运行效果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36027342/article/details/79887815