Java android.database.CursorIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19974442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:59:04  来源:igfitidea点击:

android.database.CursorIndexOutOfBoundsException

javaandroidexceptionandroid-sqliteindexoutofboundsexception

提问by Sandah Aung

The data access code in my Android app raises an exception.

我的 Android 应用程序中的数据访问代码引发异常。

Here is the code that causes the exception:

这是导致异常的代码:

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);
    ....
}

The exception raised is:

引发的异常是:

android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2 exception

The log method produces the result 2 post rows.

log 方法产生结果2 post rows

What is wrong here?

这里有什么问题?

Edit: I have added the full stack trace so that those wishing to view it can do so.

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

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.access0(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

采纳答案by Simon Dorociak

You are attempting to retrieve an item on index 2 but this index really doesn't exist (Cursor size is 2 so indexes are 0,1).

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

Change your loop:

改变你的循环:

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

Now it should work correctly.

现在它应该可以正常工作了。

Note:Don't forget to call moveToFirst()method that moves Cursor into the first record (implicitly is positioned before the first row) and prepares it for reading. This is also a handy method for testing whether Cursor is valid or not.

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

Note 2:Don't use column indexes, you can simply make a mistake in counting. Instead of use column names - this approach is generally recommended e.q. cursor.getColumnIndex("<columnName>")

注意2:不要使用列索引,你可以简单地在计数上出错。而不是使用列名 - 通常推荐这种方法 eqcursor.getColumnIndex("<columnName>")

回答by CRUSADER

You havent moved cursor index to first .. Try like this below

您还没有将光标索引移至第一个 .. 尝试如下

 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();

回答by sud007

This arises in the case our Cursorhas data but not yet pointing to first index in the data list.

如果我们的Cursor有数据但尚未指向数据列表中的第一个索引,就会出现这种情况。

To get avoid this exception, we first should check:

为了避免这个异常,我们首先应该检查:

if (!cursor.moveToFirst())
        cursor.moveToFirst();

And then carry the further actions.

然后进行进一步的操作。