BadTokenException无法在PopUpWindow中添加Window Spinner

时间:2013-06-21 07:31:46

标签: android popupwindow android-windowmanager

我知道这里有很多类似的问题,但我无法将答案转移到我的问题上(或者我找不到我需要的答案......)

在actionBar上的菜单中(在onOptionsItemSelected中)我打开一个popupWindow进行用户设置。复选框和RadioButton工作正常,但如果我在Spinner上的选项卡,将有一个致命的例外:

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

这是onOptionsItemSelected方法中的代码:

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);
    }
});

我的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" />

并在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>

修改

登录onOptionsItemSelected

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

结果:

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 个答案:

答案 0 :(得分:1)

替换

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

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

这主要是因为不良的上下文引用。