PopupWindow $BadTokenException: Unable to add window -- token null is not valid

Android

Android Problem Overview


I have the following error when showing a PopupWindow. The errors are triggered by the line:

checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);

mapView is a MapView and nothing is null. The stacktrace:

01-08 18:00:09.402: E/AndroidRuntime(27768): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.view.Window$LocalWindowManager.addView(Window.java:537)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.widget.PopupWindow.invokePopup(PopupWindow.java:988)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.widget.PopupWindow.showAtLocation(PopupWindow.java:845)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.widget.PopupWindow.showAtLocation(PopupWindow.java:809)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at com.geoloc.ActivityCheckIn.onCreate(ActivityCheckIn.java:50)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.app.Activity.performCreate(Activity.java:4465)
01-08 18:00:09.402: E/AndroidRuntime(27768): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

This is the code from my activity (that extends MapActivity)

	protected void onCreate(Bundle icicle) {
	super.onCreate(icicle);
    setContentView(R.layout.checkin);
    mapView = (MapView) findViewById(R.id.mapview);
	
	LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	checkInPopup = new PopupWindow(inflater.inflate(CHECK_IN_POPUP_LAYOUT, null, false));
	checkInPopup.setOutsideTouchable(true);
	checkInPopup.setHeight(100);
	checkInPopup.setWidth(200);
	checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gravity.CENTER_HORIZONTAL, 0, 0);
}

Thank you for sharing your thoughts

Android Solutions


Solution 1 - Android

Same problem happened with me when I try to show a popup menu in an activity.

I also got the same exception but I encountered problems and resolved it by providing the right context.

// at this line
Dialog dialog = new Dialog(getApplicationContext());

Use

YourActivityName.this instead of getApplicationContext()

and yes it worked for me may it will help someone else.

Solution 2 - Android

you are showing your popup too early. You may post a delayed runnable for showatlocation in Onresume , Give it a try

Edit: This post seems to have the same problem answered https://stackoverflow.com/questions/4187673/problems-creating-a-popup-window-in-android-activity

Solution 3 - Android

Use:

YourActivityName.this

Instead of:

getApplicationContext();

Solution 4 - Android

There are two scenarios when this exception could occur. One is mentioned by nandeesh. Other scenario is mentioned here: http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

Make sure you handle both of them

Solution 5 - Android

The correct way is to call popupwindow.show() at onWindowFocusChanged():

@Override
protected void onCreate(Bundle savedInstanceState) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.popup_window_layout, new LinearLayout(mContext), true);
    popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setContentView(view);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if (hasFocus) {
        popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
    }
}

Solution 6 - Android

A popup's parent can't itself be a popup. Both of their parents must be the same. So, if you create a popup inside a popup, you must save the parent's popup and make it a parent.

here's an example

Solution 7 - Android

Try to show the pop like below

findViewById(R.id.main_layout).post(new Runnable() {
        public void run() {
            mPopupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
            Button close = (Button) customView.findViewById(R.id.btn_ok);
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPopupWindow.dismiss();
                    doOtherStuff();
                }
            });
        }
    });

Solution 8 - Android

use this method before show

public static ViewGroup getActivityFirstLayout(Context ctx)
{
    return (ViewGroup)((ViewGroup) ActivityMaster.getActivity(ctx).findViewById(android.R.id.content)).getChildAt(0);
}

private boolean activityIsOk(Activity activity)
{
	boolean s1 = !(activity == null || activity.isFinishing());

	if(s1)
	{
		View v = LayoutMaster.getActivityFirstLayout(activity);
		return v.isShown() && ViewCompat.isLaidOut(v);
	}

	return false;
}

Solution 9 - Android

Try to use it

LayoutInflater inflater = (LayoutInflater).getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflate.from(YourActivity.this).inflate(R.layout.yourLayout, null);

Solution 10 - Android

Following many hours of search and testing i found following solution(by implementing different SO solutions) here it what didn't failed in any case i was getting crash.

     Runnable runnable = new Runnable() {
            @Override
            public void run() {
        
          //displayPopup,progress dialog or what ever action. example
        
                ProgressDialogBox.setProgressBar(Constants.LOADING,youractivityName.this);
            }};

Where logcat is indicating the crash is happening.. start a runnable .in my case at receiving broadcast.

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(!isFinishing()) {
                    new Handler().postAtTime(runnable,2000);
                }
            }
        });
                
    
     

Solution 11 - Android

try to show popup like this

new Handler().postDelayed(new Runnable(){

    public void run() {
       popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
    }

}, 200L);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionznatView Question on Stackoverflow
Solution 1 - AndroidAndroid is everything for meView Answer on Stackoverflow
Solution 2 - AndroidnandeeshView Answer on Stackoverflow
Solution 3 - AndroidMian Taimoor TahirView Answer on Stackoverflow
Solution 4 - AndroidTheManView Answer on Stackoverflow
Solution 5 - AndroidWow ChongView Answer on Stackoverflow
Solution 6 - AndroidMd Kauser AhmmedView Answer on Stackoverflow
Solution 7 - AndroidGyanendra SinghView Answer on Stackoverflow
Solution 8 - AndroidAli BagheriView Answer on Stackoverflow
Solution 9 - AndroidK.NikitaView Answer on Stackoverflow
Solution 10 - AndroidMuahmmad TayyibView Answer on Stackoverflow
Solution 11 - Androidjay patoliyaView Answer on Stackoverflow