Unable to Add Window - Token Null Is Not Valid; Is Your Activity Running

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

I had the same problem as you, it looks like you used the tutorial from http://www.piwai.info/chatheads-basics like I did. The problem is that you cannot reliably pass the current activity to the popup window, because you have no control over the current activity. It looks like there might be a unreliable way to get the current activity, but I don't recommend that.

The way I fixed it for my app was to not use the popup window, but instead make my own through the window manager.

private void showCustomPopupMenu()
{
windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.xxact_copy_popupmenu, null);
params=new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);

params.gravity=Gravity.CENTER|Gravity.CENTER;
params.x=0;
params.y=0;
windowManager2.addView(view, params);
}

If you want this to look like a popup window, just add a transparent gray view as the background and add a onClickListener to it to remove the view from the windowManager object.

I know this isn't as convenient as a popup, but from my experience, it is the most reliable way.

and don't forget to add permission in your manifest file

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

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

}
});

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 valid; is your activity running error. How can I solve it?

You're trying to show the popup too early. The view on OnCreate is still not created so try to move the popup() to OnViewCreated()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
popup()
}

or use View.post so it goes into the main thread queue and gets executed after the other pending tasks are finished

override fun onCreate(savedInstanceState: Bundle?) {
...
binding.root.post {
popup()
}
}

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