我的光标索引超出范围“请求的索引 0:大小为 0”。它只是一个用户注册和登录应用程序。当没有匹配用户名和密码的用户时,我的应用程序将崩溃。
下面是代码:
MainActivity.java
final SUSQLiteHelper dbhelper = new SUSQLiteHelper(this);
LoginData login = dbhelper.readOneUser(loginuname.getText().toString(), loginpwd.getText().toString());
if(login.getUname().toString().equals(loginuname.getText().toString()) &&
login.getPwd().toString().equals(loginpwd.getText().toString()))
{
Toast.makeText(getApplicationContext(), "Login Successfull. Welcome " + login.getUname().toUpperCase() +" !".toString(),
Toast.LENGTH_LONG).show();
}
else if(login.getUname().toString().equals(loginuname.getText().toString()) &&
!login.getPwd().toString().equals(loginpwd.getText().toString()))
{
Toast.makeText(getApplicationContext(), "Login Failed. Incorrect password !",
Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Login Failed. User doesn't exist !",
Toast.LENGTH_LONG).show(); //it never goes here if no username is found in the table
SUSQLiteHelper.java
public LoginData readOneUser(String uname, String pwd)
{
SQLiteDatabase loginUserDB = this.getReadableDatabase();
LoginData newLogin = null;
Cursor cursor = loginUserDB.query(TABLE_NAME, new String[]{TABLE_ROW_UNAME, TABLE_ROW_EMAIL, TABLE_ROW_PWD}, TABLE_ROW_UNAME + "=?",
new String[]{String.valueOf(uname)}, null, null, null);
if(cursor.moveToFirst())
{
newLogin = new LoginData(cursor.getString(0), cursor.getString(1), cursor.getString(2));
}
cursor.close();
loginUserDB.close();
return newLogin;
}
登录数据.java
此类具有 Uname、Pwd、Email 字段的 getter 和 setter 方法以及将这些字段作为参数的构造函数。