gpt4 book ai didi

在 PopupWindow 中使用 EditText 的 Android setError

转载 作者:行者123 更新时间:2023-11-29 01:56:23 26 4
gpt4 key购买 nike

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 的问题。

最佳答案

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

popup = new PopupWindow(context);

如果它是一个死的/陈旧的 Activity 或应用程序上下文,它将像这样失败。

关于在 PopupWindow 中使用 EditText 的 Android setError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14910216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com