2

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)
Community
  • 1
  • 1
Jeremy Styers
  • 497
  • 5
  • 23

3 Answers3

3
  1. I think you should not be using the application context, but the activity context.
  2. Use a dialog and not a popup. It's easier, less code to maintain, and the standard according to Google.
  3. I think there needs to be some sort of click listener on the spinner.
Buggie
  • 122
  • 1
  • 8
  • 1. No context I've used has worked(this. activity.this, getBaseContext, ect etc) 2. I will try that now 3. I added a listener but that doesnt change the error. :( Thanks though. – Jeremy Styers Jul 19 '13 at 00:29
  • That worked! That is, the dialog worked. But I have no idea why. – Jeremy Styers Jul 19 '13 at 00:46
2

Thanks to Buggie, I have found a work around. I'm using a dialog instead. It works and doesn't throw an error. I'm not sure why this works and a popup window doesn't but here's my code that works:

public void add_itinerary_clicked(View view)
{
    dialog = new Dialog(this);
    dialog.setContentView(R.layout.itinerary_add_item_page);

    Spinner airlineChoice = (Spinner) dialog.findViewById(R.id.airlineSpinner);
    Button addBtn= (Button) dialog.findViewById(R.id.finish_addItinerary);

    String[] list = new String[1];
    list = airlineMap.keySet().toArray(list);

    ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, list );
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    airlineChoice.setAdapter(spinnerAdapter);
    airlineChoice.setOnItemSelectedListener(this);

    addBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish_addItinerary_clicked();
            dialog.dismiss();
        }
    });

    dialog.show();
}
Jeremy Styers
  • 497
  • 5
  • 23
1

One thing you might try is using your activity context, rather than your application/base context. This might help the situation.

berwyn
  • 986
  • 7
  • 23