我正在创建一个非常简单的应用程序,以学习Parse功能。 一路上我意识到我必须只使用用户名而不是电子邮件,(从存档的问题中得到这个,不确定现在是否有任何更改)。 但在我的情况下,以下代码返回true,即使输入字段为空
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity);
Parse.initialize(this, "#MASKED");
emailLogin = (EditText)findViewById(R.id.loginEmail);
passwordLogin = (EditText)findViewById(R.id.loginPassword);
login_Login = (Button)findViewById(R.id.loginBtn);
signup_Login = (Button)findViewById(R.id.loginSignup);
signup_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
finish();
}
});
login_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailLogin.getText().toString().trim();
String password = passwordLogin.getText().toString().trim();
ParseUser.logInInBackground(email, password, new LogInCallback() {
@Override
public void done(ParseUser user, com.parse.ParseException e) {
if (e != null) {
// Hooray! The user is logged in.
Toast.makeText(Login_activity.this,"Sucessfully Logged in", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), HomePage.class);
startActivity(i);
finish();
} else {
// Signup failed. Look at the ParseException to see what happened.
Toast.makeText(Login_activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
AlertDialog.Builder alertDiag = new AlertDialog.Builder(Login_activity.this);
alertDiag.setMessage(e.getMessage());
alertDiag.setTitle("Error");
alertDiag.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//AlertDialog dialog =
alertDiag.create();
alertDiag.show();
}
}
});
}
});
}
答案 0 :(得分:1)
if (e != null) {
// Hooray! The user is logged in.
应该是:
if (e == null) {
// Hooray! The user is logged in.
因此,如果没有例外,则用户已成功登录。
此外,您的代码仅检查电子邮件和密码而非用户名。