由于某些原因,当我输入的用户名未找到时,应用程序会崩溃。但是当找到该用户名时,它似乎运行良好。我甚至进行了一个检查,看返回的游标是否为null。以下是代码:
public boolean isUserAuthenticated(String username, String password) {
// TODO Auto-generated method stub
boolean authenticated = false;
String[] columns = new String[] {LOGIN_USERNAME, LOGIN_PASSWORD};
String sqlUsername = "\"" + username + "\"";
Cursor c = ourDatabase.query(LOGIN_TABLE, columns, LOGIN_USERNAME + "="+ sqlUsername, null, null, null, null);
if (c != null) {
c.moveToFirst();
String passwordAttachedToUsername = c.getString(1);
if(passwordAttachedToUsername.equals(password)) {
authenticated = true;
}
}
return authenticated;
}
moveToFirst
的返回值:http://developer.android.com/reference/android/database/Cursor.html#moveToFirst%28%29 - mu is too short