【android应用】Unable to add window -- token null is not for an application

这个报错是在,接收网络断开后做一个弹窗实现时,弹窗报错。


错误日志

2019-11-25 18:14:25.120 27509-27509/com.android D/AndroidRuntime: Shutting down VM
2019-11-25 18:14:25.134 27509-27509/com.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android., PID: 27509
    java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.android.vps.basic.Receiver.NetWorkMonitorReceiver$1@ea93978
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1132)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:682)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at android.app.Dialog.show(Dialog.java:316)
        at com.android.vps.basic.Receiver.NetWorkMonitorReceiver.doNetworkAlert(NetWorkMonitorReceiver.java:129)
        at com.android.vps.basic.Receiver.NetWorkMonitorReceiver.access$200(NetWorkMonitorReceiver.java:18)
        at com.android.vps.basic.Receiver.NetWorkMonitorReceiver$1.onReceive(NetWorkMonitorReceiver.java:80)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1122)
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:915) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 

原因分析

AlertDialog创建语句public AlertDialog.Builder (Context context)中,不能使用getApplicationContext()得到的context,而必须使用Activity。

解决方案

1、将 new AlertDialog.Builder(getApplicationContext()) 改为 new AlertDialog.Builder(MainActivity.this)

其中MainActivity 为当前Activity的名称 

2、//WindowType:系统提示,出现在应用程序窗口之上

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

customDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

实例

    /**
     * 允许服务中弹窗
     */
    private void setServiceShow(int type) {
        if (type == 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                //android6.0~7.0
                mDownDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //android8.0以上
                mDownDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
            } else {
                mDownDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
            }
        } else if (type == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                //android6.0~7.0
                mAlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //android8.0以上
                mAlertDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
            } else {
                //其他
                mAlertDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
            }
        } else if (type == 2) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                //android6.0~7.0
                mInstallDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //android8.0以上
                mInstallDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
            } else {
                mInstallDialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值