如何在Android中的PopupWindow中创建一个Spinner小部件?单击Spinner时获取BadTokenException

Jef*_*f N 1 java android spinner popupwindow

我一直在网上搜索这个问题的解决方案,但不幸的是,我似乎无法找到答案.我为PopupWindow创建了一个XML文件,里面有一个Spinner.在一个按钮事件监听器中,我调用以下代码来膨胀PopupWindow并在屏幕上显示它.

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));
Run Code Online (Sandbox Code Playgroud)

单击按钮时,弹出窗口显示正常.但是,当我单击Spinner时,我在LogCat中收到以下错误.

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

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

Mik*_*ker 11

它可能有点晚了,并不完全是原始问题的答案,但我从另一个问题中发现,将以下行插入到我的微调器的xml中可以防止发生错误.

android:spinnerMode="dialog"