ChatGPT解决这个技术问题 Extra ChatGPT

Android 'Unable to add window -- token null is not for an application' exception

I get the following Android exception when I try to open a dialog. Can someone please help me understand what is going on and how can I fix this problem?

android.view.WindowManager$BadTokenException: 
  Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:509)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)
How are we supposed to know without posting some code?
Please refer this answer , the main thing to post link of this answer is there is nice conversation in comments over this answer which describe why this problem coming and what is the best way to deal with it http://stackoverflow.com/a/7229248/501483
If you use Service, read stackoverflow.com/questions/23516689/….

P
Pratik Butani

I'm guessing - are you trying to create Dialog with an application context? Something like this:

new Dialog(getApplicationContext());

This is wrong. You need to use an Activity context.

You have to try like:

new Dialog(YourActivity.this);

Thanks. But I did not use new Dialog(getApplicationContext()); I only use ' new AlertDialog.Builder(mContext);' where mContext is a referent to an activity.
Then mContext should be MyActivity.this.
The android docs (developer.android.com/guide/topics/ui/dialogs.html#CustomDialog) for creating a custom dialog have this error. Context mContext = getApplicationContext(); Dialog dialog = new Dialog(mContext); Your fix leads to Dialog dialog = new Dialog(this); Which works! Thanks
developer.android.com/guide/topics/ui/dialogs.html has been fixed (since a while). Just pointing this out for reference. The pattern has been improved as well. :)
Use getActivity() @SagarDevanga
c
codezjx

You can continue to use getApplicationContext(), but before use, you should add this flag: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT), and the error will not show.

And don't forget to add permission:

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

Yes, it's quite simple. And don't forget to add permission, I edited just now.
Can't use getActivity() in a RecyclerAdapter
That is awesome :)
How do you add the flag?
T
Tiago Almeida

In my case I was trying to create my dialog like this:

new Dialog(getApplicationContext());

So I had to change for:

new Dialog(this);

And it works fine for me ;)


Your answer is correct, however it will only work when you are in the activity scope. If you are in an inner scope, you have to use MyActivity.class instead of this because this refers to a different object.
Also, this will make sure your Dialog inherits your theme.
P
Pratik Butani

Try getParent() at the argument place of context like new AlertDialog.Builder(getParent()); Hope it will work, it worked for me.


Why use getParent()?, what's mean?
When you are showing any dialog in another class that does not extend the Activity class so you may not get the context of that class that is extending the Activity. So to get the context inside your class where you are trying to show the dialog but not extending the Activity you can use the getParent() which returns you the context of that superior activity.
Thanks @PriyankJoshi , you saved my day, it also solves the weird exception of "Android Unable to add window is not valid; is your activity running?".
How could i use the getParent() ?
Thank you! It worked in my case. I was calling this.parent.getActivity().getApplicationContext() but this.parent.getActivity() did the trick.
H
Harshid

I'm guessing - are you trying to create Dialog using.

 getApplicationContext()
 mContext which is passed by activity.

if You displaying dialog non activity class then you have to pass activity as a parameter.

Activity activity=YourActivity.this;

Now it will be work great.

If you find any trouble then let me know.


M
Matias

I tried with this in the context field:

this.getActivity().getParent()

and it works fine for me. This was from a class which extends from "Fragment":

public class filtro extends Fragment{...

c
codingwithtashi

Hello there if you are using adapter there might be a chance. All you need to know when you used any dialog in adapter,getContext(),context or activity won't work sometime.

Here is the trick I used v.getRootView().getContext() where v is the view object you are referencing.
Eg.


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(v.getRootView().getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });  
If you are getting this problem because of alert dialog.
Refer [here][1] But it is same concept.


  [1]: https://stackoverflow.com/questions/6367771/displaying-alertdialog-inside-a-custom-listadapter-class

Thank You ! for saved my precious time !! (^_^)
A
Anonymous-E

I got the same exception. what i do to fix this is to pass instance of the dialog as parameter into function and use it instead of pass only context then using getContext(). this solution solve my problem, hope it can help


G
Ganesh Garad

I solved this error by add below user-permission in AndroidManifest.xml

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

Also, Initialize Alert dialog with Activity Name:

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

For More Details, visit==> How to create Alert Dialog in Android


S
Soorya

I got this exception, when I tried to open Progress Dialog under Cordova Plugin by using below two cases,

new ProgressDialog(this.cordova.getActivity().getParent()); new ProgressDialog(this.cordova.getActivity().getApplicationContext());

Later changed like this,

new ProgressDialog(this.cordova.getActivity());

Its working fine for me.


M
Makvin

Use this and context not worked for me..but MyActivityName.this worked. Hope this helps anyone who need it.


R
Rajan

Just in case you are trying to achive showing the Dialog from a Fragment. Just use "getActivity()" method.

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