I have a spinner in my android app and I can get it to show but the sec. I click it, the app crashes and throws :
Unable to add Window -- token android.view.ViewRootImpl$Wfb@ is not valid: is your activity running?
The layout is simple. I have an activity that has a list and a button to add something to the list. When clicked, the add button open a popup window that displays the spinner, a text box and a button. Everything works fine till I click the spinner .
Now I've searched google for an hour and found these:
Android - Dynamically Adding values to Spinners
BadTokenException Unable to add Window Spinner in PopUpWindow
Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window
and more. They all seem to point to the context, however I've tried everything from using "this", to getApplicationContext, to the name of my activity.context and none of it works. I tried using the answer someone provided twice instead of just setting the contentView to the page and that made things worse (the app crashed with a null pointer exception right away).
Here's the code for the popup window (executed when the "add" button is clicked):
public void add_itinerary_clicked(View view)
{
LayoutInflater i = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = i.inflate(R.layout.itinerary_add_item_page, null);
popup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popup.showAtLocation(popupView , Gravity.CENTER, 0, 0);
Spinner airlineChoice = (Spinner) popupView.findViewById(R.id.airlineSpinner);
Button addBtn= (Button) popupView.findViewById(R.id.finish_addItinerary);
String[] list = new String[1];
list = airlineMap.keySet().toArray(list);
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(getApplicationContext(), android.R.layout.simple_spinner_item, list );
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
airlineChoice.setAdapter(spinnerAdapter);
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish_addItinerary_clicked();
popup.dismiss();
}
});
}
I'm completely at a loss at this point. If it's not the context (I've tried what others have said) then what is it?
Here's the complete error message:
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@40de1700 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:981)
at android.widget.Spinner.performClick(Spinner.java:609)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)