Clicking spinner within popup window causes Window

2019-01-20 12:08发布

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)

4条回答
叛逆
2楼-- · 2019-01-20 12:27

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

查看更多
太酷不给撩
3楼-- · 2019-01-20 12:33

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...

查看更多
戒情不戒烟
4楼-- · 2019-01-20 12:39

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

查看更多
我命由我不由天
5楼-- · 2019-01-20 12:41

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>
查看更多
登录 后发表回答