我为后端注册用户编写了一种方法。填写所有文本字段后,我可以注册用户。我的问题是,当我点击带有空文本字段的应用程序崩溃的按钮时,我写了一个if语句来检查空文本字段并显示祝酒词。
public void RegisterButtonPressed(View view) {
if(email.getText() == null || password.getText() == null || username.getText() == null){
Toast.makeText(this, "Every Fileds should be filled", Toast.LENGTH_SHORT).show();
} else {
if(email.getText() != null && password.getText() != null && username.getText() != null) {
final String emailS = email.getText().toString();
final String passwordS = password.getText().toString();
String usernameS = username.getText().toString();
BackendlessUser user = new BackendlessUser();
user.setEmail(emailS);
user.setPassword(passwordS);
user.setProperty("name",usernameS);
user.setProperty("Avatar","");
Backendless.UserService.register(user, new AsyncCallback<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser backendlessUser) {
Toast.makeText(RegisterActivity.this, "User Registration Successfull", Toast.LENGTH_SHORT).show();
Backendless.UserService.login(emailS, passwordS, new AsyncCallback<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser backendlessUser) {
Toast.makeText(RegisterActivity.this, "Logged", Toast.LENGTH_SHORT).show();
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Toast.makeText(RegisterActivity.this, "User Login Unsuccessfull: " + backendlessFault.getDetail(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
Toast.makeText(RegisterActivity.this, "User Registration Unsuccesful: " + backendlessFault.getDetail(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
答案 0 :(得分:0)
你应该检查
if(TextUtils.isEmpty(email.getText()))
而不是
if (email.getText() == null)
根据我的经验, getText()如果为空则不返回null,而是空字符串a.k.a&#34;&#34;
然而,TextUtils.isEmpty()将检查null和&#34;&#34;作为空字符串
答案 1 :(得分:0)
您只是检查字符串是否为空。如果它是空的,那么你的if
语句将无法捕获,并且仍然可能导致崩溃。你也应该检查空字符串。
if(email.getText() == null || email.getText().length() == 0 ||
password.getText() == null || password.getText.length() == 0 ||
username.getText() == null || username.getText.length() == 0){
Toast.makeText(this, "Every Fileds should be filled", Toast.LENGTH_SHORT).show();
}