-1

My application works perfectly when running in Android 8.0.0 device. But app crash when using in Àndroid 6.0.1. I checked error log, crash cause because of 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 is,

<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>

Error is,

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) 

Please help me to fix this issue, and make sure my application works in all versions of android. Thanks in advance.

Kavin-K
  • 1,948
  • 3
  • 17
  • 48

1 Answers1

-2

Try using YourActivity.this instead of context. Source