title: PopupWindow: Editext set error shows bad token error in android?
body: <p>I have implemented PopupWindow for login in that when I press Login button, I am validating edittext of username and password using setError function. But App is crash with exception <strong>"android.view.WindowManager$BadTokenException: Unable to add window"</strong></p>
  
  <p>any solution for that?</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>FATAL EXCEPTION: main
                                                                       Process: com.app.stockeyez, PID: 29076
                                                                       android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@33b573b is not valid; is your activity running?
                                                                           at android.view.ViewRootImpl.setView(ViewRootImpl.java:579)
                                                                           at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
                                                                           at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                                                                           at android.widget.PopupWindow.invokePopup(PopupWindow.java:1258)
                                                                           at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1110)
                                                                           at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1069)
                                                                           at android.widget.Editor.showError(Editor.java:445)
                                                                           at android.widget.Editor.setError(Editor.java:466)
                                                                           at android.widget.TextView.setError(TextView.java:4943)
                                                                           at android.widget.TextView.setError(TextView.java:4928)
                                                                           at com.app.stockeyez.MainActivity.validate(MainActivity.java:484)
                                                                           at com.app.stockeyez.MainActivity.access$100(MainActivity.java:52)
                                                                           at com.app.stockeyez.MainActivity$3.onClick(MainActivity.java:278)
                                                                           at android.view.View.performClick(View.java:5207)
                                                                           at android.view.View$PerformClick.run(View.java:21168)
                                                                           at android.os.Handler.handleCallback(Handler.java:746)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)</p>
  </blockquote>