Android - 无法添加窗口

时间:2016-01-25 07:44:27

标签: android

我正在使用AwesomeValidation验证我的应用中的所有表单。

除了用户点击PopupWindow时具有TextView的表单以外,其他所有表单都适用。

当我点击验证其中的表单的PopupWindow按钮时,它什么也没做。但在我输入EditText内的任何内容后,它显示出来 android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@15f1d142 is not valid; is your activity running?

这是弹出窗口的代码:

View.OnClickListener phoneReinputHandler = new View.OnClickListener() {

        public void onClick(View arg0) {
            /*Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class);
            backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout);
            //backDim.setVisibility(View.VISIBLE);
            startActivity(intent);*/

            mainLayout = (RelativeLayout)findViewById(R.id.activity_signup_step_two_mainLayout);
            backDim = (RelativeLayout)findViewById(R.id.bac_dim_layout);

            backDim.setVisibility(View.VISIBLE);

            int width = displayMetrics.widthPixels;
            int height = displayMetrics.heightPixels;
            inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //View popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout);

            PopupWindow pw = new PopupWindow(
                    inflater.inflate(R.layout.popup_handphone, null, false),
                    (int)(width * .8),
                    (int)(height*.35),
                    true);
            //pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            pw.setOutsideTouchable(true);

            RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities);
            //popupFunctionalitiesWrapper.setGravity(Gravity.CENTER);
            int popupFunctionalitiesWrapperWidth = layoutResize.width(70);
            RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams();
            popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth;
            popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER);
            popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams);

            userPhonePopup = (EditText)pw.getContentView().findViewById((R.id.popup_handphone_phoneNumber));
            mAwesomeValidation.addValidation(userPhonePopup, Patterns.PHONE, "Phone number must not be empty");
            userPhoneCfmPopup = (EditText)pw.getContentView().findViewById((R.id.popup_handphone_phoneNumberConfirm));

            Button buttonPhoneCodeResend = (Button)pw.getContentView().findViewById(R.id.popup_handphone_phoneNumberButton);
            buttonPhoneCodeResend.setOnClickListener(new View.OnClickListener() {

                AwesomeValidation resendPhoneValidation = new AwesomeValidation(ValidationStyle.COLORATION);

                @Override
                public void onClick(View v) {
                    Context context = v.getContext();

                    if(!((Activity) context).isFinishing())
                    {
                        if (!userPhonePopup.getText().toString().equals(userPhoneCfmPopup.getText().toString())) {
                            resendPhoneValidation.addValidation(userPhoneCfmPopup, "/^" + userPhonePopup.getText().toString() + "$/", "Nomor handphone harus sama");
                        }
                        resendPhoneValidation.validate();
                    }

                }
            });

            pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
            pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    backDim.setVisibility(View.GONE);
                }
            });


        }
    };

Context的问题?

0 个答案:

没有答案