android.database.CursorIndexOutOfBoundsException [英] android.database.CursorIndexOutOfBoundsException

查看:445
本文介绍了android.database.CursorIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是导致异常的代码:

  String getPost =SELECT * FROM+ TABLE_POST +;; 
Cursor result = getReadableDatabase()。rawQuery(getPost,null);
列表帖子= 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);
....
}

提出的异常是:

  android.database.CursorIndexOutOfBoundsException:请求索引2,大小为2个例外

日志方法产生结果 2个后续行



这里有什么问题?



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

  11-14 09:57:50.538:W / dalvikvm(4710):threadid = 1:线程退出未捕获异常(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:无法启动活动ComponentInfo {com.innolabmm.software.collaboration / com.innolabmm.software.collaboration.PostCommentActivity}:android.database.CursorIndexOutOfBoundsException:请求索引2,大小为2
11-14 09 :57:50.608:E / AndroidRuntime(4710):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-14 09:57:50.608:E / AndroidRuntime(4710):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-14 09:57:50.608:E / AndroidRuntime(4710):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):在android.os.Handler。 dispatchMessage(Handler.java:99)
11-14 09:57:50.608:E / AndroidRuntime(4710):在android.os.Looper.loop(Looper.java:137)
11-14 09:57:50.608:E / AndroidRuntime(4710):android.app.ActivityThread.main(ActivityThread.java:5041)
11-14 09:57:50.608:E / AndroidRuntime(4710) .lang.reflect.Method.invokeNative(Native Method)
11-14 09:57:50.608:E / AndroidRuntime(4710):java.lang.reflect.Method.invoke(Method.java:511)
11-14 09: 57:50.608:E / AndroidRuntime(4710):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方法)
11-14 09:57:50.608:E / AndroidRuntime(4710):引起:android.database.CursorIndexOutOfBoundsException:请求索引2,大小为2
11-14 09: 57:50.608:E / AndroidRuntime(4710):在android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
11-14 09:57:50.608:E / AndroidRuntime(4710):在android.database .AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
11-14 09:57:50.608:E / AndroidRuntime(4710):android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
11-14 09:57:50.608:E / AndroidRuntime(4710):在com.innolabmm.software.collaboration.data.Collaborat ionDbHandler.fetchAllPost(CollaborationDbHandler.java:362)
11-14 09:57:50.608:E / AndroidRuntime(4710):在com.innolabmm.software.collaboration.PostCommentActivity.onCreate(PostCommentActivity.java:48)
11-14 09:57:50.608:E / AndroidRuntime(4710):android.app.Activity.performCreate(Activity.java:5104)
11-14 09:57:50.608:E / AndroidRuntime (4710):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-14 09:57:50.608:E / AndroidRuntime(4710):在android.app.ActivityThread.performLaunchActivity(ActivityThread。 java:2144)
11-14 09:57:50.608:E / AndroidRuntime(4710):... 11更多


解决方案

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

更改您的循环:

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

现在它应该正常工作。



注意:不要忘记调用 moveToFirst()将Cursor移动到第一个记录的方法(隐含地位于第一行之前)并准备阅读。这也是测试光标是否有效的方法。



注2:不要使用列索引,可以简单的做计数错误而不是使用列名称 - 这种方法通常建议e.q. cursor.getColumnIndex(< columnName>)


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

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.

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

解决方案

You are attempting to retrieve item on index 2 but this index really doesn't exist (Cursor size is 2 so indexes are 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 works correctly.

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

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>")

这篇关于android.database.CursorIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆