【问题标题】:SQLite high scores, CursorIndexOutOfBounds - AndroidSQLite 高分,CursorIndexOutOfBounds - Android
【发布时间】:2012-12-19 02:17:48
【问题描述】:

我在下面的代码中评论了发生 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 输出。

【问题讨论】:

  • 请注意,您错误地使用了getColumnIndex()。你应该使用c.getColumnIndex(SCORE)
  • 而不是在争论周围加上双引号?
  • 正确,不要再在结果中加 1。

标签: java android sqlite cursor indexoutofboundsexception


【解决方案1】:

您需要先致电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);

请参阅example,了解如何使用光标。

【讨论】:

  • 嘿@Nambar,我更新了我的代码,但在同一行仍然遇到同样的错误。我认为错误信息可能略有不同。在我的开篇文章中更新了 LogCat 和代码。
  • @user1866707:正如我在回答中评论的那样,如果您不执行 hasNext() 并且没有可用的行,您将看到您现在看到的异常。阅读我的答案中链接的教程以避免它。添加 do/while 条件。
  • 工作!我现在已经接受了这个答案。我已经添加了 do/while 条件。但是现在c.moveToFirst() 总是返回 false。我知道我的insert() 确实插入了数据库。我认为我的 RANK/AUTOINCREMENT 现在可能工作正常。
  • @user1866707:我会先尝试对查询进行硬编码,并确保在深入之前返回行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多