这个报错是在,接收网络断开后做一个弹窗实现时,弹窗报错。
错误日志
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));
}
}
}