PopupWindow:Editext设置错误在android中显示错误的令牌错误?

时间:2017-06-19 04:20:57

标签: android android-edittext

我已经实现了PopupWindow登录,当我按下Login按钮时,我正在使用setError函数验证用户名和密码的edittext。但是App崩溃,异常" android.view.WindowManager $ BadTokenException:无法添加窗口"

任何解决方案?

Activity activity;
PopupWindow popupLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    activity = this;

}


   @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_login:
                    if (popupLogin == null || !popupLogin.isShowing())
                        showLogin();

                    return true;

                default:
                    return super.onOptionsItemSelected(item);
            }
        }


    private void showLogin() {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popupView = inflater.inflate(R.layout.dialog_login,
                    null);

            popupLogin = new PopupWindow(
                    popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            Display display = activity.getWindowManager().getDefaultDisplay();
            popupLogin.setFocusable(true);
            popupLogin.update();
            Point size = new Point();
            display.getSize(size);


            Rect rectangle = new Rect();
            Window window = activity.getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
            int topbar = rectangle.top + toolbar.getHeight();
            edtBroker = (EditText) popupView.findViewById(R.id.edit_broker_code);
            edtUname = (EditText) popupView.findViewById(R.id.edit_username);
            edtPwd = (EditText) popupView.findViewById(R.id.edit_pwd);
            // popupLogin.setAnimationStyle(R.style.PopupWindowAnimation);
            popupView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupLogin.dismiss();

                }
            });
            popupView.findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    if (validate()) {
                        popupLogin.dismiss();
                        doLogin(edtBroker.getText().toString().trim(), edtUname.getText().toString().trim(), edtPwd.getText().toString().trim());
                    }
                }
            });

            popupLogin.showAtLocation(toolbar, Gravity.NO_GRAVITY, 0, topbar);


        }


 private boolean validate() {

        if (TextUtils.isEmpty(edtBroker.getText().toString())) {
            edtBroker.setError(getString(R.string.plz_broker_code));//getting exception here
            edtBroker.requestFocus();
            return false;
        }
        if (TextUtils.isEmpty(edtUname.getText().toString())) {
            edtUname.setError(getString(R.string.plz_enter_username));
            edtUname.requestFocus();
            return false;
        }
        if (TextUtils.isEmpty(edtPwd.getText().toString())) {
            edtPwd.setError(getString(R.string.plz_enter_password));
            edtPwd.requestFocus();
            return false;
        }
        if (!NetworkStatus.getConnectivityStatus(activity)) {
            CommonUtility.showAlertToast(activity, getString(R.string.check_internet), "");
            return false;
        }

        return true;
    }

例外:

  

致命的例外:主要                                                                      过程:com.app.stockeyez,PID:29076                                                                      android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@33b573b无效;你的活动在运行吗?                                                                          在android.view.ViewRootImpl.setView(ViewRootImpl.java:579)                                                                          在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)                                                                          在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)                                                                          在android.widget.PopupWindow.invokePopup(PopupWindow.java:1258)                                                                          在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1110)                                                                          在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1069)                                                                          在android.widget.Editor.showError(Editor.java:445)                                                                          在android.widget.Editor.setError(Editor.java:466)                                                                          在android.widget.TextView.setError(TextView.java:4943)                                                                          在android.widget.TextView.setError(TextView.java:4928)                                                                          在com.app.stockeyez.MainActivity.validate(MainActivity.java:484)                                                                          在com.app.stockeyez.MainActivity.access $ 100(MainActivity.java:52)                                                                          在com.app.stockeyez.MainActivity $ 3.onClick(MainActivity.java:278)                                                                          在android.view.View.performClick(View.java:5207)                                                                          在android.view.View $ PerformClick.run(View.java:21168)                                                                          在android.os.Handler.handleCallback(Handler.java:746)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:148)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5443)                                                                          at java.lang.reflect.Method.invoke(Native Method)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:1)

我认为您在活动中使用了applicationContext()

而不是getApplicationContext(),只需使用ActivityName.this

我认为它会解决您的问题。