Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 634
znat Avatar asked Jan 08 '12 23:01

znat


1 Answers

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.

like image 144
Android is everything for me Avatar answered Oct 21 '22 12:10

Android is everything for me