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

问题描述

我抬头一对夫妇的职位,同样的问题,但似乎无法来解决我的问题。我用我的整个应用纺纱和他们工作的罚款。这时候,我尝试将popupwindow内使用一个微调,我选择它的时候得到一个错误。弹出窗口是添加引用和我已经声明一个全局的ViewGroup变量(即vg_references),它可以用于检索在弹出窗口上的组件。在code弹出窗口如下。

  LayoutInflater吹气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vg_references =(ViewGroup中)inflater.inflate(R.layout.reference,空,假);pw_references =新PopupWindow(vg_references,
    this.getWindowManager()。getDefaultDisplay()。的getWidth()/ 100 * 75,
    this.getWindowManager()。getDefaultDisplay()。的getHeight()/ 100 * 50,
    真正);
pw_references.setFocusable(真);
pw_references.showAtLocation((查看)view.getParent(),Gravity.CENTER,0,0);this.populateReferenceView();

这然后调用一个函数来填充popupwindow内的组件(例如文本框,纺纱,布局,等等...)。这部分功能是填充,使其具有从数据库字符串列表。

  //查找微调
微调spinReferenceSourceTypes =(微调)vg_references.findViewById(R.id.spin_referencesSourceTypes);//检索参考源类型的列表,并创建一个数组适配器附加到列表
ArrayAdapter<串GT; DataAdapter的=新ArrayAdapter<串GT;(
    此,android.R.layout.simple_spinner_item,databaseHelper.getReferenceSourceTypes());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinReferenceSourceTypes.setAdapter(DataAdapter的);//如果只有1在下拉列表项,禁用下拉列表
如果(spinReferenceSourceTypes.getCount()2){
    spinReferenceSourceTypes.setEnabled(假);
}
其他{
    spinReferenceSourceTypes.setEnabled(真);
}

当我点击这个微调,该方案具有以下错误崩溃。任何人都可以请帮我解决这个问题。谢谢大家。

  18 12-04:43:41.507:E / AndroidRuntime(30504):致命异常:主要
12-04 18:43:41.507:E / AndroidRuntime(30504):$ android.view.WindowManager BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@414c67c8无效;是您的活动运行?
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.ViewRootImpl.setView(ViewRootImpl.java:520)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:140)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.Window $ LocalWindowManager.addView(Window.java:537)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.widget.PopupWindow.invokePopup(PopupWindow.java:992)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:901)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.widget.Spinner $ DropdownPopup.show(Spinner.java:764)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.widget.Spinner.performClick(Spinner.java:457)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.view.View $ PerformClick.run(View.java:14152)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.os.Handler.handleCallback(Handler.java:605)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.os.Handler.dispatchMessage(Handler.java:92)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.os.Looper.loop(Looper.java:137)
12-04 18:43:41.507:E / AndroidRuntime(30504):在android.app.ActivityThread.main(ActivityThread.java:4514)
12-04 18:43:41.507:E / AndroidRuntime(30504):在java.lang.reflect.Method.invokeNative(本机方法)
12-04 18:43:41.507:E / AndroidRuntime(30504):在java.lang.reflect.Method.invoke(Method.java:511)
12-04 18:43:41.507:E / AndroidRuntime(30504):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
12-04 18:43:41.507:E / AndroidRuntime(30504):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-04 18:43:41.507:E / AndroidRuntime(30504):在dalvik.system.NativeStart.main(本机方法)


解决方案

我能得到这个工作的唯一方法是使用一个对话而非PopupWindow。它工作正常,这样

I've looked up a couple of posts for the same problem but can't seem to resolve my issue. I've used spinners throughout my application and they are working fine. It's when I try to use a spinner within a popupwindow, I get an error when selecting it. The popup window is to add references and I've declared a global ViewGroup variable (i.e. vg_references) which can be used to retrieve the components on the popup window. The code to popup the window is as follows.

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vg_references = (ViewGroup)inflater.inflate(R.layout.reference, null, false);

pw_references = new PopupWindow(vg_references,
    this.getWindowManager().getDefaultDisplay().getWidth()/100 * 75,
    this.getWindowManager().getDefaultDisplay().getHeight()/100 * 50,
    true);
pw_references.setFocusable(true);
pw_references.showAtLocation((View)view.getParent(), Gravity.CENTER, 0, 0);

this.populateReferenceView();

This then calls a function to populate the components within the popupwindow (e.g. textfields, spinners, layouts, etc...). Part of this function is to populate a spinner with a list of strings from a database.

// Find the spinner
Spinner spinReferenceSourceTypes = (Spinner) vg_references.findViewById(R.id.spin_referencesSourceTypes);

// Retrieve the list of reference source types and create an array adapter to attach to the list
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
    this, android.R.layout.simple_spinner_item, databaseHelper.getReferenceSourceTypes());
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinReferenceSourceTypes.setAdapter(dataAdapter);

// If there's only 1 item in the dropdown list, disable the dropdown list
if(spinReferenceSourceTypes.getCount() < 2) {
    spinReferenceSourceTypes.setEnabled(false);
}
else {
    spinReferenceSourceTypes.setEnabled(true);
}

When I click on this spinner, the program crashes with the following error. Can anyone please help me with this problem. Thank you all

12-04 18:43:41.507: E/AndroidRuntime(30504): FATAL EXCEPTION: main
12-04 18:43:41.507: E/AndroidRuntime(30504): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@414c67c8 is not valid; is your activity running?
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:520)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.Window$LocalWindowManager.addView(Window.java:537)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.widget.PopupWindow.invokePopup(PopupWindow.java:992)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:901)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.widget.Spinner$DropdownPopup.show(Spinner.java:764)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.widget.Spinner.performClick(Spinner.java:457)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.view.View$PerformClick.run(View.java:14152)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.os.Handler.handleCallback(Handler.java:605)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.os.Looper.loop(Looper.java:137)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at android.app.ActivityThread.main(ActivityThread.java:4514)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at java.lang.reflect.Method.invokeNative(Native Method)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at java.lang.reflect.Method.invoke(Method.java:511)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-04 18:43:41.507: E/AndroidRuntime(30504):    at dalvik.system.NativeStart.main(Native Method)
解决方案

The only way I could get this to work was to use a Dialog rather than a PopupWindow. It works fine that way

这篇关于弹出的窗口中点击微调导致窗口管理$ BadTokenException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:52