gpt4 book ai didi

java - 无法给变量赋值

转载 作者:行者123 更新时间:2023-11-30 02:46:41 25 4
gpt4 key购买 nike

我是一个非常新的 android 开发者,我正面临一个奇怪的问题。我正在尝试创建一个简单的验证测试来定义输入是否有效且用户名不在我们手中,或者相反。我尝试了各种方法,比如创建一个静态 bollean 'isValid' 变量并对其进行赋值,或者使用 setValid() 之类的方法对其进行赋值,但似乎没有任何效果。

我确定代码正在处理(调试)但没有任何变化,它卡在 false 上。

请与我分享你的智慧。

public class Registration extends Fragment {
public boolean isValid=false;

public Registration() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.registration, container,
false);

return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

Button bDone = (Button) getActivity().findViewById(R.id.bDone);
bDone.setOnClickListener(new View.OnClickListener() {
// pressed when finished entering personal details
@Override
public void onClick(View v) {

final EditText fName = (EditText) getActivity().findViewById(R.id.etFirstName);
final EditText lName = (EditText) getActivity().findViewById(R.id.etLastName);
final EditText id = (EditText) getActivity().findViewById(R.id.etID);
final EditText email = (EditText) getActivity().findViewById(R.id.etEmail);
final EditText uName = (EditText) getActivity().findViewById(R.id.etRegUsername);
final EditText pWord = (EditText) getActivity().findViewById(R.id.etRegPassword);

ParseQuery<ParseObject> query = ParseQuery.getQuery("Account");

String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
String emailAddress;

ParseObject dsAccount = new ParseObject("Account");
dsAccount.put("firstName", fName.getText().toString());
dsAccount.put("lastName", lName.getText().toString());
dsAccount.put("ID", id.getText().toString());
dsAccount.put("Email", email.getText().toString());
dsAccount.put("username", uName.getText().toString());
dsAccount.put("password", pWord.getText().toString());

emailAddress = email.getText().toString();

if (fName.getText().toString().length() == 0
|| lName.getText().toString().length() == 0
|| id.getText().toString().length() == 0
|| email.getText().toString().length() == 0
|| uName.getText().toString().length() == 0
|| pWord.getText().toString().length() == 0) {
// at least one of the fields is empty
Toast.makeText(getActivity().getApplicationContext(),"One or more fields are missing",Toast.LENGTH_SHORT).show();
} else if (!emailAddress.matches(emailPattern)) {
// email address not valid
Toast.makeText(getActivity().getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
} else {
// email address valid

query.whereEqualTo("username", uName.getText().toString());
query.whereEqualTo("password", pWord.getText().toString());
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject Account, ParseException e) {
if (Account == null) {
isValid=true;//***this is the code that doesnt happen**
} else {
Toast.makeText(getActivity().getApplicationContext(),"Username is already in use",Toast.LENGTH_SHORT).show();
}
}
});
}
if (isValid){//**this is the code that supposed to respond**
dsAccount.saveInBackground();
Toast.makeText(getActivity().getApplicationContext(),"New account saved", Toast.LENGTH_SHORT).show();
}
}

});

}
}

最佳答案

您发布的 fragment 的问题是 query.getFirstInBackground 在后台执行,并且您在调用 done 时检查 isValid 值。所以你可能想执行

  dsAccount.saveInBackground();
Toast.makeText(getActivity().getApplicationContext(),"New account saved", Toast.LENGTH_SHORT).show();

done(ParseObject Account, ParseException e) { 被调用时,而不是设置标志。另外,只有 UI Thread 可以触及 ui

关于java - 无法给变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24887877/

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