Clicking spinner within popup window causes WindowManager$BadTokenException

前端 未结 4 1556
你的背包
你的背包 2020-12-11 21:34

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 wh

相关标签:
4条回答
  • 2020-12-11 21:45

    The following is how the popup code is being called. I've declared the variable for the PopupWindow and ViewGroup so it can be used throughout the class

    private PopupWindow pw_references;
    private vg_references;
    

    The code that calls the PopupWindow is called within an event handler for a button.

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

    This button is inside a linearlayout (seperate layout file) that is then included within a framelayout on the current screen. Included something like the following.

     <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>
    
    0 讨论(0)
  • 2020-12-11 21:50

    Can you try by replacing following line:

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

    with

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

    where this refer to the current activity reference. Just to see if this make any difference...

    0 讨论(0)
  • 2020-12-11 21:58

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

    0 讨论(0)
  • 2020-12-11 22:06

    I had this problem in my work, and wasted about 2 days trying to find a solution to it. But, I didn't find a solution from the web.

    The solution was to specify the Spinner mode to be dialog.

    from XML layout:

    android:spinnerMode="dialog"
    

    or from java code:

    Spinner(Context context, int mode)
    

    I hope my answer was helpful

    0 讨论(0)
提交回复
热议问题