在PopupWindow中使用EditText的Android setError

时间:2013-02-16 12:19:39

标签: android

02-16 13:06:21.702: W/dalvikvm(5138): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
02-16 13:06:21.713: E/AndroidRuntime(5138): FATAL EXCEPTION: main
02-16 13:06:21.713: E/AndroidRuntime(5138): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@405c3bd0 is not valid; is your activity running?
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.view.ViewRoot.setView(ViewRoot.java:533)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at     android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:907)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:819)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.widget.TextView.showError(TextView.java:3438)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.widget.TextView.setError(TextView.java:3409)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.widget.TextView.setError(TextView.java:3373)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at com.example.zoopiekuj.MenuActions$5.onClick(MenuActions.java:185)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.view.View.performClick(View.java:2538)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.view.View$PerformClick.run(View.java:9152)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.os.Handler.handleCallback(Handler.java:587)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.os.Looper.loop(Looper.java:130)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at android.app.ActivityThread.main(ActivityThread.java:3687)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at java.lang.reflect.Method.invoke(Method.java:507)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-16 13:06:21.713: E/AndroidRuntime(5138):     at dalvik.system.NativeStart.main(Native Method)

所以我在PopupWindow中有EditText输入,我试图通过使用一些检查来验证这个字段,然后用setError方法设置错误。而且我犯了以上错误:/任何想法是什么问题?

       final float scale = context.getResources().getDisplayMetrics().density;
   int popupWidth  =  (int) (300 * scale + 0.5f);
   int popupHeight = (int) (345 * scale + 0.5f);

   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popupreg);
   LayoutInflater layoutInflater = (LayoutInflater) context
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   layout = layoutInflater.inflate(R.layout.logregpopup, viewGroup);
       popup = new PopupWindow(context);
       popup.setContentView(layout);
       popup.setWidth(popupWidth);
       popup.setHeight(popupHeight);
       popup.setFocusable(true);
       popup.setBackgroundDrawable(new BitmapDrawable());
       popup.showAtLocation(layout, Gravity.BOTTOM,0,90);

       Button btnreg = (Button) layout.findViewById(R.id.btnregister);
       btnreg.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View arg0) {
                 progressDialog = ProgressDialog.show(context, "Ładowanie", "poczekaj proszę !");

                 getmethod = 1;

                 int err = 0;

                 TextHelper thelper = new TextHelper();

                 EditText login     = (EditText) layout.findViewById(R.id.reglogin);
                 EditText email     = (EditText) layout.findViewById(R.id.regemail);
                 EditText pass1     = (EditText) layout.findViewById(R.id.regpass1);
                 EditText pass2     = (EditText) layout.findViewById(R.id.regpass2);



                 String l  = login.getText().toString();
                 String e  = email.getText().toString();
                 String p1 = pass1.getText().toString();
                 String p2 = pass2.getText().toString();

                 if(l.equals("") || l.length() < 3){
                     err++;
                     login.setError("Login musi mieć co najmniej 3 znaki");
                 }

我可以从登录字段中获取文本而没有任何问题,所以这只是setError的错误。

1 个答案:

答案 0 :(得分:1)

此行中的上下文是调用活动吗?

popup = new PopupWindow(context);

如果它是死/陈旧活动或应用程序上下文,它将会失败。