2013-01-09 21 views
0

我正在獲取遊標索引越界「索引0請求:大小爲0」。它只是一個用戶註冊和登錄應用程序。當沒有與用戶名和密碼匹配的用戶時,我的應用程序就會崩潰。CursorIndexOutOfBoundsException無法解析

下面是代碼:

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; 
} 

LoginData.java

該類具有Uname,Pwd,Email字段的getter和setter方法以及將這些字段作爲參數的構造方法。

+0

錯誤日誌請 – ePeace

+0

@ePeace這裏是我的日誌... 01-09 19:12:16.440:E/AndroidRuntime(308):android.database.CursorIndexOutOfBoundsException:指數0請求,大小爲0 –

回答

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 
    } 
+1

謝謝!它的工作:) –

0

在功能readOneUser(),檢查:

if(cursor.moveToFirst() && !cursor.isAfterLast()) { 
    // 
}