Android 'Unable to Add Window -- Token Null Is Not for an Application' Exception

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! : )

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

You should use Activity.this instead of getApplicationContext()

$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

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);

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.

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.

Unable to add window -- token null is not for an application from Service

You cannot show Dialogs from a Service context.

I suggest you to open an Activity that actually shows the dialog, or take a look at this answer to learn how to show system alerts.

https://stackoverflow.com/a/19269931/1725088

Unable to add window -- token null is not valid; is your activity running? in AutoCompletetextView

There is some dialog or other kind of window being added before your activity is shown to user , thus there is no token available to added a new window ,try using post on your view it might help you to solve your error.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

autoexheader.post(new Runnable() {
@Override
public void run() {

autoexheader.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() == 0) {
autoexheader.showDropDown();
}

}

@Override
public void afterTextChanged(Editable editable) {
if (editable.length() == 0) {
autoexheader.showDropDown();
}
}
});

}
});


Related Topics



Leave a reply



Submit