如何在Android中的PopupWindow内创建Spinner小部件?单击Spinner时获取BadTokenException [英] How to create a Spinner widget inside of a PopupWindow in Android? Get BadTokenException when clicking on Spinner

查看:184
本文介绍了如何在Android中的PopupWindow内创建Spinner小部件?单击Spinner时获取BadTokenException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索该问题的解决方案,但不幸的是,我似乎找不到答案.我为其中带有Spinner的PopupWindow创建了XML文件.在按钮事件侦听器内部,我调用以下代码来填充PopupWindow并将其显示在屏幕上.

I've been searching the web for the solution to this problem, but, unfortunately, I can't seem to find the answer. I created an XML file for a PopupWindow with a Spinner inside of it. Inside a button event listener, I call the following code to inflate the PopupWindow and display it on the screen.

LayoutInflater inflater = getLayoutInflater();
settings_layout = inflater.inflate(R.layout.setting_popout, (ViewGroup) findViewById(R.id.setting_popout));

// Creates a popup window of required width and height, and displays
// the popup in the center of the screen.
pw_settings = new PopupWindow(settings_layout, 400, 470, true); 
pw_settings.showAtLocation(settings_layout, Gravity.CENTER, 0, 0);

spColors = (Spinner) settings_layout.findViewById(R.id.linecolor);

// Sets the initial values of the color spinner and the listener
ArrayAdapter<CharSequence> adapter_color = 
    ArrayAdapter.createFromResource(this, R.array.colors_array, android.R.layout.simple_spinner_item);
adapter_color.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spColors.setAdapter(adapter_color);
spColors.setSelection(adapter_color.getPosition(over.color));

单击按钮时,弹出窗口会很好显示.但是,当我单击微调器时,在LogCat中出现以下错误.

When clicking the button, the popup window shows up fine. However, I get the following error in LogCat when I click on the Spinner.

android.view.WindowManager $ BadTokenException:无法添加窗口-令牌android.view.ViewRootImpl$W@41402a90无效;您的活动正在进行吗? ...

android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@41402a90 is not valid; is your activity running? ...

我不确定自己在做什么错.任何帮助将不胜感激!谢谢!

I'm not sure what I'm doing wrong. Any help would be greatly appreciated! Thank you!

推荐答案

可能有点晚了,并不是对原始问题的确切回答,但是我在这里的另一个问题中发现,将以下行插入xml我的微调器阻止了该错误的发生.

It may be a bit late, and not exactly an answer to the original question, but I found from another question here that inserting the following line into the xml for my spinner prevented that error from occurring.

android:spinnerMode="dialog"

这篇关于如何在Android中的PopupWindow内创建Spinner小部件?单击Spinner时获取BadTokenException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆