Spinner inside popup not opening

2019-07-27 13:58发布

问题:

I trying to open a spinner inside popup but when I click at it nothing happens.

private void showTwitterPopUp()
    {
        View popUpView = getLayoutInflater().inflate(R.layout.twitter_popup, null); 
        mpopup = new PopupWindow(MainActivity.this);
        mpopup.setWidth(this.getWindowManager().getDefaultDisplay().getWidth());
        mpopup.setHeight(this.getWindowManager().getDefaultDisplay().getHeight()/2);
        mpopup.setContentView(popUpView);
        mpopup.setAnimationStyle(android.R.style.Animation_Dialog);  
        mpopup.setFocusable(true);
        mpopup.showAtLocation(popUpView, Gravity.CENTER, 200, 200);    
        mpopup.setBackgroundDrawable(new BitmapDrawable());
        mpopup.setOutsideTouchable(true);

        account = (Button)  popUpView.findViewById(R.id.button_create_account);
        bday    = (Button)  popUpView.findViewById(R.id.button_birthday);
        email   = (EditText)popUpView.findViewById(R.id.edittext_email);
        sex     = (Spinner) popUpView.findViewById(R.id.spinner_sex);
        cancel  = (Button)  popUpView.findViewById(R.id.button_cancel);

        ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item , AppConstants.SEX);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sex.setAdapter(new CustomSpinnerAdapter(adapter1,getString(R.string.sex),this));
        }

on clicking spinner and then dismissing window giving exception.

07-16 13:07:35.434: E/AndroidRuntime(23444): java.lang.IllegalStateException: Calling with view android.widget.PopupWindow$PopupViewContainer@42e3c588 but the ViewAncestor is attached to null
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:396)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.WindowManagerImpl$CompatModeWrapper.removeViewImmediate(WindowManagerImpl.java:174)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.widget.PopupWindow.dismiss(PopupWindow.java:1256)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.widget.ListPopupWindow.dismiss(ListPopupWindow.java:612)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.widget.Spinner.onDetachedFromWindow(Spinner.java:411)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.View.dispatchDetachedFromWindow(View.java:11815)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2534)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2532)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewRootImpl.dispatchDetachedFromWindow(ViewRootImpl.java:2640)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewRootImpl.doDie(ViewRootImpl.java:3993)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.ViewRootImpl.die(ViewRootImpl.java:3976)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:390)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.WindowManagerImpl$CompatModeWrapper.removeViewImmediate(WindowManagerImpl.java:174)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.widget.PopupWindow.dismiss(PopupWindow.java:1256)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at com.incbit.mygivingbook.MainActivity.onClick(MainActivity.java:235)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.View.performClick(View.java:4110)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.view.View$PerformClick.run(View.java:17001)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.os.Handler.handleCallback(Handler.java:615)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.os.Looper.loop(Looper.java:137)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at android.app.ActivityThread.main(ActivityThread.java:4854)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
07-16 13:07:35.434: E/AndroidRuntime(23444):    at dalvik.system.NativeStart.main(Native Method)

回答1:

First, you need to initialise the PopupWindow with the popupView which you created:

mpopup = new PopupWindow(popUpView);

Then you need to pass the parent view to showAtLocation instead of the popUpView:

View layout = findViewById(R.layout.twitter_layout);
mpopup.showAtLocation(layout, Gravity.CENTER, 200, 200);

From developer.android.com:

Parameters: parent a parent view to get the getWindowToken() token from