2013-01-04 41 views
0

我在兩個景點的代碼中都評論了IOOF異常正在發生。我知道肯定通過測試數據庫創建正確,我可以插入它。我正在嘗試在測試之後將所有內容整合在一起,而現在它不工作。 LogCat也在下面。SQLite高分,CursorIndexOutOfBounds - Android

DatabaseHelper.java

private static final String RANK = "_id"; 
private static final String SCORE = "score"; 
private static final String PERCENTAGE = "percentage"; 
private static final String SUM = "sum"; 

public long getScore(int rank) { 
    Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + RANK + " = " + rank, null); 
     c.moveToFirst(); 
    long i = c.getInt(c.getColumnIndex("SCORE") + 1); 
    c.close(); 
    return i; 
} 

public int getPercentage(int rank) { 
    Cursor c = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + " WHERE " + RANK + " = " + rank, null); 
     c.moveToFirst(); 
    int i = c.getInt(c.getColumnIndex("PERCENTAGE") + 1); //Line 56 
    c.close(); 
    return i; 
} 

public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE " + TABLE + " (" 
     + RANK + " INTEGER PRIMARY KEY AUTOINCREMENT," 
     + SCORE + " LONG," 
     + PERCENTAGE + " INTEGER," 
     + SUM + " INTEGER" 
     + ");"); 

Highscores.java

onCreate(...) { 

    r1p.setText("Row1 P: " + dh.getPercentage(1)); //Row 90 
    r1s.setText("Row1 S: " + dh.getScore(1)); 

    r2p.setText("Row2 P: " + dh.getPercentage(2)); 
    r2s.setText("Row2 S: " + dh.getScore(2)); 

    r3p.setText("Row3 P: " + dh.getPercentage(3)); 
    r3s.setText("Row3 S: " + dh.getScore(3)); 

    //etc... 

logcat的輸出

01-04 00:30:28.462: E/AndroidRuntime(5440): FATAL EXCEPTION: main 
01-04 00:30:28.462: E/AndroidRuntime(5440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Highscores}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.os.Looper.loop(Looper.java:137) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at java.lang.reflect.Method.invoke(Method.java:511) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at dalvik.system.NativeStart.main(Native Method) 
01-04 00:30:28.462: E/AndroidRuntime(5440): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at com.example.test.DatabaseHelper.getPercentage(DatabaseHelper.java:58) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at com.example.test.Highscores.onCreate(Highscores.java:90) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.Activity.performCreate(Activity.java:5104) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
01-04 00:30:28.462: E/AndroidRuntime(5440):  ... 11 more 

編輯:我綜合了什麼建議,現在的錯誤是在同一行,但我覺得有點不同。我上面更新了我的LogCat輸出。

+1

請注意,您正在使用'getColumnIndex()'不正確。你應該使用'c.getColumnIndex(SCORE)'。 – Sam

+0

而不是把雙引號圍繞爭論? – Matt

+1

正確,不要再將結果加1。 – Sam

回答

4

您在撥打Cursor之前需要先致電moveToFirst()

Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + RANK + " = " + rank, null); 
    c.moveToFirst(); 
    //It is always better to do hasNext() check here. Just to make sure rows are available otherwise it may throw exception. 
    long i = c.getInt(c.getColumnIndex("SCORE") + 1); 

看到這個example關於如何使用光標。

+0

嘿@Nambar,我更新了我的代碼,並且仍然在同一行上發生同樣的錯誤。我認爲錯誤信息可能會略有不同。在我的開放文章中更新了LogCat和代碼。 – Matt

+1

@ user1866707:正如我在我的回答中所評論的,如果你沒有使用hasNext()和NO行,你將會看到你現在看到的異常。閱讀我在答案中鏈接的教程以避免它。添加do/while條件。 – kosa

+0

工作!我現在接受了這個答案。我已經添加了do/while條件。但是現在''''c.moveToFirst()''''總是返回false。我知道我的''''insert()''''確實插入了數據庫。我在想我的RANK/AUTOINCREMENT現在可能正常工作。 – Matt