本文介绍了BadTokenException无法添加窗口中微调PopUpWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多类似的问题,但我不能够答案传输到我的问题(或我没有找到答案,我需要...)

在(在onOptionsItemSelected)我的动作条菜单我打开用户设置一个popupWindow。复选框,单选按钮做工精细,但如果在spinner我选项卡,将有一个致命异常:

 致命异常:主要
android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@42766f00无效;是您的活动运行?

这是在 onOptionsItemSelected 法code:

  LayoutInflater inflaterSettings = LayoutInflater.from(背景);
最终的LinearLayout llSettings =(的LinearLayout)inflaterSettings.inflate(R.layout.actionbar_menu_settings_popup,NULL);
微调微调=(微调)llSettings .findViewById(R.id.spinner_action_settings_intervall_values​​);
ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(这一点,R.array.action_settings_intervall_values​​,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(适配器);
spinner.setOnItemSelectedListener(新OnItemSelectedListener()
{
    @覆盖
    公共无效onItemSelected(适配器视图<>母公司,观景,INT POS,长I​​D){} TODO
    @覆盖
    公共无效onNothingSelected(适配器视图<>母公司){// TODO}
});
PopupWindow弹出=新PopupWindow(背景);
popUp.setContentView(llSettings);
llSettings.post(新的Runnable()
{
    @覆盖
    公共无效的run()
    {
        popUp.showAtLocation(llSettings,Gravity.CENTER,0,0);
        INT宽度= getResources()getDisplayMetrics()widthPixels。;
        INT高度= getResources()getDisplayMetrics()heightPixels。;
        宽度=(INT)(宽* 0.9);
        身高=(INT)(高* 0.9);
        popUp.update(宽度,高度);
    }
});

我actionbar_menu_settings_popup.xml:

 <微调
    机器人:ID =@ + ID / spinner_action_settings_intervall_values​​
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:drawSelectorOnTop =真
    机器人:项=@阵列/ action_settings_intervall_values​​/>

和在string.xml:

 <字符串数组名=action_settings_intervall_values​​>
    <项目> 5℃/项目>
    <项目> 15℃; /项目>
    <项目> 30℃/项目>
    <项目> 45℃; /项目>
    <项目> 60℃/项目>
    <项目> 120℃,/项目>
    <项目> 180°; /项目>
< /字符串数组>

修改

做了一个登录 onOptionsItemSelected

  Log.i(MainActivity#onCreateOptionsItems,背景:+背景);
Log.i(MainActivity#onCreateOptionsItems,getApplicationContext:+ getApplicationContext());
Log.i(MainActivity#onCreateOptionsItems,这样的:这+);

结果:

  10月6日至21日:40:58.185:I / MainActivity#onCreateOptionsItems(14463):背景:de.xy.app.activities.MainActivity@42743818
10月6日至21日:40:58.185:I / MainActivity#onCreateOptionsItems(14463):getApplicationContext:android.app.Application@42741d48
10月6日至21日:40:58.185:I / MainActivity#onCreateOptionsItems(14463):此:de.xy.app.activities.MainActivity@42743818


解决方案

替换行

  ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(这一点,R.array.action_settings_intervall_values​​,android.R.layout.simple_spinner_item);

  ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(getApplicationContext(),R.array.action_settings_intervall_values​​,android.R.layout.simple_spinner_item);

这会发生,主要是因为糟糕的情况下参考。

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
解决方案

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.

这篇关于BadTokenException无法添加窗口中微调PopUpWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 09:14