BadTokenException Unable to add Window Spinner in PopUpWindow

你离开我真会死。 提交于 2019-12-12 02:16:43

问题


I know there are a lot of similar questions here, but I'm not able to transfer the answers to my problem (or I didn't find THE answer I need...)

In my menu on actionBar (in onOptionsItemSelected) I open a popupWindow for user settings. Checkbox and RadioButtons work fine, but if I tab on the Spinner, there will be a FATAL EXCEPTION:

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@42766f00 is not valid; is your activity running?

This is the code in the onOptionsItemSelected Method:

LayoutInflater inflaterSettings = LayoutInflater.from(context);
final LinearLayout llSettings = (LinearLayout) inflaterSettings.inflate(R.layout.actionbar_menu_settings_popup, null);
Spinner spinner = (Spinner) llSettings .findViewById(R.id.spinner_action_settings_intervall_values);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.action_settings_intervall_values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){ TODO}
    @Override
    public void onNothingSelected(AdapterView<?> parent) { // TODO }
});
PopupWindow popUp = new PopupWindow(context);
popUp.setContentView(llSettings);
llSettings.post(new Runnable()
{
    @Override
    public void run()
    {
        popUp.showAtLocation(llSettings, Gravity.CENTER, 0, 0);
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        width = (int) (width * 0.9);
        height = (int) (height * 0.9);
        popUp.update(width, height);
    }
});

My actionbar_menu_settings_popup.xml:

<Spinner
    android:id="@+id/spinner_action_settings_intervall_values"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:entries="@array/action_settings_intervall_values" />

and in string.xml:

<string-array name="action_settings_intervall_values">
    <item>5</item>
    <item>15</item>
    <item>30</item>
    <item>45</item>
    <item>60</item>
    <item>120</item>
    <item>180</item>
</string-array>

EDIT

Made a Log in onOptionsItemSelected:

Log.i("MainActivity#onCreateOptionsItems", "context: " + context);
Log.i("MainActivity#onCreateOptionsItems", "getApplicationContext: " + getApplicationContext());
Log.i("MainActivity#onCreateOptionsItems", "this: " + this);

result:

06-21 10:40:58.185: I/MainActivity#onCreateOptionsItems(14463): context: de.xy.app.activities.MainActivity@42743818
06-21 10:40:58.185: I/MainActivity#onCreateOptionsItems(14463): getApplicationContext: android.app.Application@42741d48
06-21 10:40:58.185: I/MainActivity#onCreateOptionsItems(14463): this: de.xy.app.activities.MainActivity@42743818

回答1:


Replace the line

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.action_settings_intervall_values, android.R.layout.simple_spinner_item);

To

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.action_settings_intervall_values, android.R.layout.simple_spinner_item);

This occurs mostly because of bad context reference.



来源:https://stackoverflow.com/questions/17229929/badtokenexception-unable-to-add-window-spinner-in-popupwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!