【问题标题】:Clicking spinner within popup window causes WindowManager$BadTokenException在弹出窗口中单击微调器会导致 WindowManager$BadTokenException
【发布时间】:2013-12-05 14:37:34
【问题描述】:

我已经针对相同的问题查找了几篇帖子,但似乎无法解决我的问题。我在整个应用程序中都使用了微调器,它们工作正常。当我尝试在弹出窗口中使用微调器时,选择它时出现错误。弹出窗口用于添加引用,我声明了一个全局 ViewGroup 变量(即 vg_references),可用于检索弹出窗口上的组件。弹出窗口的代码如下。

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

然后调用一个函数来填充弹出窗口中的组件(例如文本字段、微调器、布局等)。此功能的一部分是使用数据库中的字符串列表填充微调器。

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

当我单击此微调器时,程序崩溃并出现以下错误。谁能帮我解决这个问题。谢谢大家

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)

【问题讨论】:

  • 你是在调用代码从后台线程显示弹出窗口吗?
  • 嗨@PrafulBhatnagar,不,我是从按钮的onClick事件中调用它
  • 能否贴出调用这个弹窗代码的代码...
  • 嗨@PrafulBhatnagar 我刚刚发布了下面调用它的代码

标签: java android select spinner


【解决方案1】:

我在工作中遇到了这个问题,并浪费了大约 2 天的时间来寻找解决方案。但是,我没有从网上找到解决方案。

解决方案是将Spinner 模式指定为dialog

来自 XML 布局:

android:spinnerMode="dialog"

或来自java代码:

Spinner(Context context, int mode)

希望我的回答对你有帮助

【讨论】:

  • 谢谢。它节省了我的时间。
【解决方案2】:

我可以让它工作的唯一方法是使用 Dialog 而不是 PopupWindow。这样就可以正常工作了

【讨论】:

    【解决方案3】:

    以下是弹出代码的调用方式。我已经为 PopupWindow 和 ViewGroup 声明了变量,因此它可以在整个类中使用

    private PopupWindow pw_references;
    private vg_references;
    

    调用 PopupWindow 的代码在按钮的事件处理程序中被调用。

    public void ibtn_references_Click(View view) {
        ...
        // 1st section of code in the original post
    }
    

    此按钮位于一个线性布局(单独的布局文件)内,该布局随后包含在当前屏幕上的框架布局内。包括以下内容。

     <FrameLayout
        android:id="@+id/lay_rowButtons"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:background="@drawable/backgroud_border"
        android:padding="10dp" >
    
        <include android:id="@+id/buttons" layout="@layout/buttons" />
    
    </FrameLayout>
    

    【讨论】:

      【解决方案4】:

      您可以尝试替换以下行吗:

      pw_references = new PopupWindow(vg_references, 
          this.getWindowManager().getDefaultDisplay().getWidth()/100 * 75,
          this.getWindowManager().getDefaultDisplay().getHeight()/100 * 50,
          true);
      

      pw_references = new PopupWindow(this);
      pw_references.setWidth(this.getWindowManager().getDefaultDisplay().getWidth()/100 * 75);
      pw_references.setHeight(this.getWindowManager().getDefaultDisplay().getHeight()/100 * 50);
      pw_references.setContentView(vg_references);
      

      其中this 指的是当前的activity 引用。只是看看这是否有什么不同......

      【讨论】:

      • 非常感谢。我尝试将 this 替换为对当前 Activity 的引用,但在构造函数定义中,没有构造函数接受 Activity 的参数。这只是视图或上下文。我已经尝试将您的建议更改为将其更改为查看,但仍然没有任何乐趣。这就是你改变这个的意思吗?
      • 嗨@PrafulBhatnagar,我似乎仍然无法解决这个问题,只是想知道您对如何解决还有其他想法,非常感谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多