我在下面的代码中评论了IOOF异常正在发生的两个地方。我确信通过测试db正确创建并且我能够插入它。我试图在测试后将所有内容集成在一起并且现在不能正常工作。 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输出。
答案 0 :(得分:4)
在开始使用moveToFirst()
之前,您需要致电Cursor
。
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);
有关如何使用Cursor的信息,请参阅此example。