0

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

在我的 actionBar 菜单中(在 onOptionsItemSelected 中),我为用户设置打开了一个弹出窗口。Checkbox 和 RadioButtons 工作正常,但如果我在 Spinner 上进行选项卡,将会有一个 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?

这是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
4

1 回答 1

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

这主要是因为错误的上下文引用。

于 2013-06-21T08:04:42.300 回答