[英]SQLite high scores, CursorIndexOutOfBounds - Android
我在下面的代碼中都在注釋IOOF異常。 通過測試,我肯定知道數據庫創建正確並且可以插入數據庫。 我試圖在測試后將所有內容集成在一起,但現在無法正常工作。 LogCat也在下面。
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輸出。
在獲取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);
請參閱此示例 ,了解如何使用游標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.