在具有较低版本操作系统的设备中运行时,应用程序崩溃

时间:2018-01-25 15:56:24

标签: android android-alertdialog

Android 8.0.0设备中运行时,我的应用程序运行正常。但是在Àndroid 6.0.1中使用时,应用会崩溃。我检查了错误日志,崩溃原因是因为AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle);
        builder.setMessage("Would you like to enable it?")
                .setTitle("No Internet Connection")
                .setPositiveButton(" Enable Internet ", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
                        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(dialogIntent);
                    }
                });

        alert = builder.create();
        alert.setCancelable(false);
        alert.show();

MyAlertDialogStyle是,

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#FFC107</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:background">#4CAF50</item>
</style>

错误是,

E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.app.com, PID: 22952                                           java.lang.RuntimeException: Unable to start receiver my.app.com.receivers.NetworkChangeReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:3641)
 at android.app.ActivityThread.access$2000(ActivityThread.java:221)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:158)
 at android.app.ActivityThread.main(ActivityThread.java:7224)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
 at android.view.ViewRootImpl.setView(ViewRootImpl.java:853)
 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
 at android.app.Dialog.show(Dialog.java:350)
 at my.app.com.preferences.GlobalModel.displayLoadingAlert(Global.java:130)
 at my.app.com.receivers.NetworkChangeReceiver.onReceive(NetworkChangeReceiver.java:27)
 at android.app.ActivityThread.handleReceiver(ActivityThread.java:3634)
 at android.app.ActivityThread.access$2000(ActivityThread.java:221) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1876) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:158) 
 at android.app.ActivityThread.main(ActivityThread.java:7224) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

请帮我修复此问题,并确保我的应用程序适用于所有版本的android。提前谢谢。

1 个答案:

答案 0 :(得分:-2)

尝试使用YourActivity.this代替contextSource