EditText setError not working in PopupWindow

試著忘記壹切 提交于 2019-12-10 15:56:13

问题


I had popup window with custom layout which has edittext. I'm trying to show error message in edittext with setError method, but it's giving following exception.

android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@211ffd68 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:579)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1008)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:968)
at android.widget.Editor.showError(Editor.java:334)
at android.widget.Editor.setError(Editor.java:355)
at android.widget.TextView.setError(TextView.java:4654)
at android.widget.TextView.setError(TextView.java:4639)
at com.ebusiness.worldofjobs.helpers.HelperEditText.isEmailValid(HelperEditText.java:38)
at com.ebusiness.worldofjobs.activities.LoginActivity$4$2.onClick(LoginActivity.java:149)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

Popup window:

View popupView = LayoutInflater.from(LoginActivity.this).inflate(R.layout.popup_reset_password, null);

EdiText etSmaple = (EditText) popupView.findViewById(R.id.etSmample);
Button buttonSample = (Button) popupView.findViewById(R.id.buttonSample);

final PopupWindow popupWindow = new PopupWindow(popupView, 600, 400);

popupWindow.setFocusable(true);

buttonSample.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {

             // Here's the exception raising
             etSample.setError("Nothing...");
      }
});

popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);

回答1:


It's not about the setError(String error), maybe you added your PopupWindow during the wrong time.

Reference



来源:https://stackoverflow.com/questions/33969026/edittext-seterror-not-working-in-popupwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!