android.database.CursorIndexOutOfBoundsException

San*_*ung 8 java android exception indexoutofboundsexception android-sqlite

我的Android应用中的数据访问代码引发了异常.

以下是导致异常的代码:

String getPost = "SELECT * FROM " + TABLE_POST + ";";
Cursor result = getReadableDatabase().rawQuery(getPost, null);
List posts = new ArrayList();{
Log.d("count", result.getCount() + " post rows");
while (result != null && result.moveToNext());
    Post post = new Post();
    post.setPostId(result.getInt(0));
    posts.add(post);
    ....
}
Run Code Online (Sandbox Code Playgroud)

提出的例外是:

android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2 exception
Run Code Online (Sandbox Code Playgroud)

log方法产生结果2 post rows.

这有什么不对?

编辑:我已经添加了完整的堆栈跟踪,以便那些希望查看它的人可以这样做.

11-14 09:57:50.538: W/dalvikvm(4710): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
11-14 09:57:50.608: E/AndroidRuntime(4710): FATAL EXCEPTION: main
11-14 09:57:50.608: E/AndroidRuntime(4710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.innolabmm.software.collaboration/com.innolabmm.software.collaboration.PostCommentActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.os.Looper.loop(Looper.java:137)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at dalvik.system.NativeStart.main(Native Method)
11-14 09:57:50.608: E/AndroidRuntime(4710): Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at com.innolabmm.software.collaboration.data.CollaborationDbHandler.fetchAllPost(CollaborationDbHandler.java:362)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at com.innolabmm.software.collaboration.PostCommentActivity.onCreate(PostCommentActivity.java:48)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.Activity.performCreate(Activity.java:5104)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-14 09:57:50.608: E/AndroidRuntime(4710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-14 09:57:50.608: E/AndroidRuntime(4710):     ... 11 more
Run Code Online (Sandbox Code Playgroud)

Sim*_*iak 36

您正在尝试检索索引2上的项目,但该索引确实不存在(游标大小为2,因此索引为0,1).

改变你的循环:

if (result != null && result.moveToFirst()){
    do {
       Post post = new Post();
       post.setPostId(result.getInt(0));
       posts.add(post);
       ....
    } while (result.moveToNext());
}
Run Code Online (Sandbox Code Playgroud)

现在它应该正常工作.

注意:不要忘记调用moveToFirst()将Cursor移动到第一个记录中的方法(隐式位于第一行之前)并准备读取.这也是测试Cursor是否有效的方便方法.

注意2:不要使用列索引,你可以简单地计算错误.而不是使用列名称 - 通常建议使用此方法cursor.getColumnIndex("<columnName>")


CRU*_*DER 6

你没有将光标索引移动到第一个..尝试如下

 String getPost = "SELECT * FROM " + TABLE_POST + ";";
    Cursor result = getReadableDatabase().rawQuery(getPost, null);
    List posts = new ArrayList();{
    Log.d("count", result.getCount() + " post rows");

    if (result .moveToFirst()) {
        do {
             Post post = new Post();
             post.setPostId(result.getInt(0));
             posts.add(post);
              ....
        } while (result .moveToNext());
    }
    result .close();
Run Code Online (Sandbox Code Playgroud)


sud*_*007 5

在我们的Cursor有数据但尚未指向数据列表中的第一个索引的情况下会出现这种情况.

为避免此异常,我们首先应检查:

if (!cursor.moveToFirst())
        cursor.moveToFirst();
Run Code Online (Sandbox Code Playgroud)

然后进行进一步的行动.

  • 也许我错过了一些东西,但为什么要调用cursor.moveToFirst()两次? (2认同)
  • 不,你实际上调用了两次。为给您结果所做的工作是相同的。因此,在 IF 条件内时,cursor.moveToFirst() 不会更改其功能。您不需要第二次调用,因为 IF 块中的第一次调用将完成您需要的所有操作(显然,不要反转结果)。 (2认同)