Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeException on AlertDialog.show()

I keep getting an exception when I want to display an alertdialog at the beginning of an activity. I can't figure out why? I really would appreciate an explanation.

The code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    if (!isInternetConnectionAvailable()) {
        AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create();
        alert.setMessage("TEST DIALOG!!!");
        alert.show();
    }
...

03-19 16:04:33.933: ERROR/AndroidRuntime(15145): FATAL EXCEPTION: main 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.droidgroup.THMInfo/de.droidgroup.THMInfo.Activities.StartActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.os.Looper.loop(Looper.java:123) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.main(ActivityThread.java:4627) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at java.lang.reflect.Method.invoke(Method.java:521) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at dalvik.system.NativeStart.main(Native Method) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.view.ViewRoot.setView(ViewRoot.java:509) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.Dialog.show(Dialog.java:241) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at de.droidgroup.THMInfo.Activities.StartActivity.onCreate(StartActivity.java:82) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): ... 11 more

like image 359
Beasly Avatar asked Dec 04 '22 09:12

Beasly


1 Answers

Use this instead of getApplicationContext(). More info here.

like image 81
bigstones Avatar answered Dec 28 '22 09:12

bigstones