Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:26:53+00:00 2026-06-17T17:26:53+00:00

I am trying to create a dialog box that pops up when an activity

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:26:53+00:00Added an answer on June 17, 2026 at 5:26 pm

    Just change

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

    to

    AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a hint dialog box that informs the user about
I am trying to create a dialog box that shows the process of loading
I am trying to create a custom dialog box that displays an image in
I'm trying to create a Dialog box that will display a video (quick tutorial)
I am trying to create a dialog box that will appear only if the
I am trying to squeeze the entire height of a dialog box I created
I'm trying to create a modal confirmation dialog box. I'd like it to work
I am trying to create a custom dialog box on the click of an
i am trying to use Alert Dialog Box and Async Task in the activity
In my application i am trying to open a dialog box when activity started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.