弹出窗口内的微调器没有打开

时间:2013-07-16 07:23:45

标签: android popup android-spinner

我试图在弹出窗口中打开一个微调器但是当我点击它时没有任何反应。

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

点击微调器,然后解除窗口给出异常。

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 个答案:

答案 0 :(得分:1)

首先,您需要使用您创建的PopupWindow初始化popupView

mpopup = new PopupWindow(popUpView);

然后您需要将视图传递给showAtLocation而不是popUpView:

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

来自developer.android.com:

  

参数:父视图从父视图中获取getWindowToken()标记