Android Error: Unable to add window -- token null is not for an application
To add AlertDialog
to your Activity
or Fragment
you have to use your Activity
's instance, not your application's which you are doing in your code. Create your AlertDialog
like this :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
And that should do the trick for you! : )
Dialog throwing Unable to add window — token null is not for an application”
On the following line:
mWindowManager = (WindowManager) getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
remove the reference to the application context
mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
The context of the activity will be sufficient.
Alert Dialog Exception Unable to add windows -- token null is not for an application, what context should I give?
To show a Dialog
you need to use the Activity
context. Here you're using the Application
context which is incorrect.
productsAdapter = new ProductsAdapter(getApplicationContext(), 0);
To make it work change the above line to use the Activity
context. Since this call is made inside an Activity
you can use:
productsAdapter = new ProductsAdapter(this, 0);
But this is not the best way to do so because your parameter type is Context
and not Acticity
and as it's happening to you, the context may be incorrect.
A better idea is to provide a click listener callback to your Activity
and manage the Dialog
inside the Activity
instead of the Adapter
.
In AlertDialog - Unable to add window -- token null is not valid; is your activity running?
- Pass the current activity context instead Application context in AlertDialog.Builder().
- Set the AlertDialog type to WindowManagerTypes.ApplicationPanel intead of Toast
AlertDialog.Builder builder = new AlertDialog.Builder(context);//current activity.
b.Window.SetType(WindowManagerTypes.ApplicationPanel);
$BadTokenException: Unable to add window -- token null is not for an application - Kotlin
It causes error, because it can't find the current view's context
to show the AlertDialog
So what you need to do is replacing val context = applicationContext
to
val context = view.context
Unable to add window -- token null is not for an application inside OnReceive of Broadcast Receiver
Just removing my receiver from manifest.xml solved my issue. Thanks.
Passing the Context always fails - Unable to add window -- token null is not valid; is your activity running?
I may be missing something but you could override onAttach
in the DialogFragment class instead of passing the context in through the constructor.
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
Related Topics
How to Store Image as Blob in Sqlite & How to Retrieve It
Your Content Must Have a Listview Whose Id Attribute Is 'Android.R.Id.List'
Getting Exception "Illegalstateexception: Can Not Perform This Action After Onsaveinstancestate"
Drawing a Line/Path on Google Maps
Android: Expand/Collapse Animation
Understanding Fragment'S Setretaininstance(Boolean)
How to Save Users Score in Firebase and Retrieve It in Real-Time in Android Studio
Picasso V/S Imageloader V/S Fresco VS Glide VS Coil
Android: How to Get String from Resources Using Its Name
How to Use Recyclerview Inside Nestedscrollview
Scrollview Vertical and Horizontal in Android
How to Detect When Wifi Connection Has Been Established in Android
Example: Communication Between Activity and Service Using Messaging
How to Use the New Sd Card Access API Presented For Android 5.0 (Lollipop)
Android: View.Setid(Int Id) Programmatically - How to Avoid Id Conflicts