Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
Asked Answered
I

16

309

I'm trying to open a dialog window, but every time I try to open it it throws this exception:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: 
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:460)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:238)
  at android.app.Activity.showDialog(Activity.java:2413)

I'm creating it by calling showDialog with the display's id. The onCreateDialog handler logs fine and I can step through it without an issue, but I've attached it since it seems like I'm missing something:

@Override
public Dialog onCreateDialog(int id)
{
    Dialog dialog;
    Context appContext = this.getApplicationContext();
    switch(id)
    {
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setContentView(R.layout.rename);
            dialog.setTitle("Rename " + noteName);
            break;
        default:
            dialog = null;
            break;
    }
    return dialog;      
}

Is there something missing from this? Some questions have talked about having this problem when creating a dialog from onCreate, which happens because the activity isn't created yet, but this is coming from a call from a menu object, and the appContext variable seems like it is correctly populated in the debugger.

Intersidereal answered 14/4, 2010 at 4:55 Comment(0)
G
612

Instead of : Context appContext = this.getApplicationContext(); you should use a pointer to the activity you're in (probably this).

I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com :(

Grandaunt answered 14/4, 2010 at 17:18 Comment(12)
It's also reported as a bug (although it wasn't when the user posted the question): code.google.com/p/android/issues/detail?id=11199Toll
Just in case this helps anyone - use myActivity.this as your context in a dialog.Shanleigh
This question and answer turns 3 years old in 2 days. I'm still getting reputation, so i guess Google still haven't fixed their docs...Grandaunt
This has been fixed in (developer.android.com/guide/topics/ui/dialogs.html)Provenance
Great, guess this question can go away and finally die of old age then.Grandaunt
Replaced getApplicationContext() with this , its works fine. ThanxDekko
@Grandaunt Nope. Not gonna die soon. Take an upvote from a user you helped.Ridglea
No, it's still a problem, especially since people will randomly advice you to use getApplicationContext().Loree
So as a clarification, you should use the Activity Context to launch a Dialog rather than some other one.Mechanical
This is April of 2016 , and still this exception making application crash on dialog initiation.Osteotome
August '16 here, still got it after trying this answer.Case
This is definitely what solved my problem, though I used the getApplicationContext(); for the reason that it is recommended to reduced JAVA Heap Memory, and i learned that it is not recommended to use as an instance when it is used for UI/Widgets...Winsor
T
79

You cannot display an application window/dialog through a Context that is not an Activity. Try passing a valid activity reference

Trocki answered 14/4, 2010 at 6:44 Comment(2)
how? I tried activity.this and activity.getBaseContext() but no avail. Any help?Sizable
Got it. Directly pass your activity name in it. Without .this.Sizable
H
46

Ditto on the getApplicationContext thing.

The documents on the android site says to use it, but it doesn't work...grrrrr :-P

Just do:

dialog = new Dialog(this); 

"this" is usually your Activity from which you start the dialog.

Hanshaw answered 8/5, 2010 at 3:26 Comment(0)
T
45

Android documents suggests to use getApplicationContext();

but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...

Ex:

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

or

AlertDialog.Builder builder = new  AlertDialog.Builder((Your Activity).this);
Telecommunication answered 8/2, 2012 at 7:34 Comment(3)
This helped me out massively. I was trying to create a dialog from within another dialog and just having "AlertDialog.Builder(this);" was giving an error. Thanks!Controvert
(ActivityName.this) is especially useful when trying to create a dialog inside onClick of a buttonSalesman
My problem is that I'm building a ProgressDialog inside an AlertDialog inside of an Adapter... I can't get it to work.Cliff
F
17

Instead of getApplicationContext(), just use ActivityName.this

Flushing answered 23/4, 2015 at 10:53 Comment(0)
A
13

I had a similar issue where I had another class something like this:

public class Something {
  MyActivity myActivity;

  public Something(MyActivity myActivity) {
    this.myActivity=myActivity;
  }

  public void someMethod() {
   .
   .
   AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
   .
   AlertDialog alert = builder.create();
   alert.show();
  }
}

Worked fine most of the time, but sometimes it crashed with the same error. Then I realise that in MyActivity I had...

public class MyActivity extends Activity {
  public static Something something;

  public void someMethod() {
    if (something==null) {
      something=new Something(this);
    }
  }
}

Because I was holding the object as static, a second run of the code was still holding the original version of the object, and thus was still referring to the original Activity, which no long existed.

Silly stupid mistake, especially as I really didn't need to be holding the object as static in the first place...

Auditor answered 3/8, 2011 at 13:52 Comment(0)
A
12

Just change it into

AlertDialog.Builder alert_Categoryitem = 
    new AlertDialog.Builder(YourActivity.this);

Instead of

AlertDialog.Builder alert_Categoryitem = 
    new AlertDialog.Builder(getApplicationContext());
Arbitrament answered 8/5, 2013 at 5:49 Comment(0)
L
9

Another solution is to set the window type to a system dialog:

dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

This requires the SYSTEM_ALERT_WINDOW permission:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

As the docs say:

Very few applications should use this permission; these windows are intended for system-level interaction with the user.

This is a solution you should only use if you require a dialog that's not attached to an activity.

Levana answered 13/7, 2015 at 16:2 Comment(1)
This is now deprecated flag from API level 26. Because it allows the developer to play with system window which is not good from user perspective.Parhe
Q
4

Don't use getApplicationContext() on declaring dialouge

Always use this or your activity.this

Quiche answered 4/3, 2015 at 11:35 Comment(0)
M
2

For nested dialogs this issue is very common, It works when

AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);

is used instead of

mDialogBuilder = new AlertDialog.Builder(getApplicationContext);

this alternative.

Monkhmer answered 25/8, 2014 at 17:18 Comment(0)
B
2

This Worked for me--

new AlertDialog.Builder(MainActivity.this)
        .setMessage(Html.fromHtml("<b><i><u>Spread Knowledge Unto The Last</u></i></b>"))
        .setCancelable(false)
        .setPositiveButton("Dismiss",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                }).show();

Use

ActivityName.this
Boondoggle answered 6/12, 2015 at 10:31 Comment(0)
N
0

You can also do this

public class Example extends Activity {
    final Context context = this;
    final Dialog dialog = new Dialog(context);
}

This worked for me !!

Nonce answered 7/9, 2013 at 10:53 Comment(0)
F
0
public class Splash extends Activity {

    Location location;
    LocationManager locationManager;
    LocationListener locationlistener;
    ImageView image_view;
    ublic static ProgressDialog progressdialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        progressdialog = new ProgressDialog(Splash.this);
           image_view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                        locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener);
                        Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show();

                            progressdialog.setMessage("getting Location");
                            progressdialog.show();
                            Intent intent = new Intent(Splash.this,Show_LatLng.class);
//                          }
        });
    }

Text here:-
use this for getting activity context for progressdialog

 progressdialog = new ProgressDialog(Splash.this);

or progressdialog = new ProgressDialog(this);

use this for getting application context for BroadcastListener not for progressdialog.

progressdialog = new ProgressDialog(getApplicationContext());
progressdialog = new ProgressDialog(getBaseContext());
Fibster answered 30/1, 2016 at 13:22 Comment(0)
B
0

As it's said, you need an Activity as context for the dialog, use "YourActivity.this" for a static context or check here for how to use a dynamic one in a safe mode

Blumenfeld answered 22/2, 2016 at 10:13 Comment(0)
I
0

Try to reset dialog window's type to

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

Don't forget to use the permission android.permission.SYSTEM_ALERT_WINDOW

Indeclinable answered 3/3, 2016 at 8:7 Comment(0)
T
0

The best and the safest way to show a 'ProgressDialog' in an AsyncTask, avoiding memory leak problem is to use a 'Handler' with Looper.main().

    private ProgressDialog tProgressDialog;

then in the 'onCreate'

    tProgressDialog = new ProgressDialog(this);
    tProgressDialog.setMessage(getString(R.string.loading));
    tProgressDialog.setIndeterminate(true);

Now you r done with the setup part. Now call 'showProgress()' and 'hideProgress()' in AsyncTask.

    private void showProgress(){
        new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                tProgressDialog.show();
            }
        }.sendEmptyMessage(1);
    }

    private void hideProgress(){
        new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                tProgressDialog.dismiss();
            }
        }.sendEmptyMessage(1);
    }
Tuatara answered 23/2, 2017 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.