Dialog Throwing "Unable to Add Window - Token Null Is Not For an Application" With Getapplication() as Context

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?

  1. Pass the current activity context instead Application context in AlertDialog.Builder().
  2. 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



Leave a reply



Submit