Javacadabra
Javacadabra

Reputation: 5758

Error creating Dialog Box

I am trying to create a dialog box that pops up when an activity is created. This is the method where I create the dialog box:

public Dialog createDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());

    builder.setMessage("Order Information");
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            //Create order object in here
            Toast.makeText(getApplication(), "order created!", Toast.LENGTH_SHORT).show();
        }
    });
    return builder.create();
}

then I call it in my onCreate method as shown here:

Dialog d = createDialog();
    d.show();

This is the error I am getting:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

This is my log cat errors:

01-21 16:10:45.533: E/AndroidRuntime(9228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.os.Looper.loop(Looper.java:137)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at dalvik.system.NativeStart.main(Native Method)
01-21 16:10:45.533: E/AndroidRuntime(9228): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Dialog.show(Dialog.java:281)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.example.waitron5.MainActivity.onCreate(MainActivity.java:52)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Activity.performCreate(Activity.java:5104)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 16:10:45.533: E/AndroidRuntime(9228):     ... 11 more

any idea what I am doing wrong, I understand it might be that the DialogBox is not getting a reference to the activity from which it was created. I was thinking of storing a reference to the activity in a static variable and calling it that way but I read that this is not recommended due to memory leaks.

any advice is much appreciated!

Upvotes: 0

Views: 285

Answers (1)

baldguy
baldguy

Reputation: 2082

Just change

AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());

to

AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this);

Upvotes: 1

Related Questions