0

我的光标索引超出范围“请求的索引 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 方法以及将这些字段作为参数的构造函数。

4

2 回答 2

0

在函数 readOneUser() 中,检查:

if(cursor.moveToFirst() && !cursor.isAfterLast()) {
     //
}
于 2013-01-09T15:18:46.060 回答
0

CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

这意味着cursor.moveToFirst() returns falseCursor is empty。因此,尝试添加用于检查光标是否为空的代码段,并取决于修改您的逻辑。

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!=null) //Check whether cursor is null or not
  {
   if(cursor.moveToFirst())
   {
     newLogin = new LoginData(cursor.getString(0), cursor.getString(1), cursor.getString(2));
   }
  }
cursor.close();
loginUserDB.close();
return newLogin;
 }

  LoginData login = dbhelper.readOneUser(loginuname.getText().toString(), loginpwd.getText().toString());
  if(login!=null)
   {
     //check for incorrect username or password
    }
  else
   {
     //Notify that no Username available with these Username
   }
于 2013-01-09T15:19:55.430 回答