我在下面的代码中评论了发生 IOOF 异常的两个地方。我通过测试确定数据库创建正确并且我能够插入其中。我正在尝试在测试后将所有内容集成在一起,但现在无法正常工作。LogCat 也在下面。
数据库助手.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"
+ ");");
高分.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 输出。