Android 光标索引超出范围,原因不明

标签 android sqlite cursor

我的光标有问题,我知道错误,但不知道如何解决这个问题。 让我解释一下我在进入代码模式之前首先尝试实现的想法。

我在 SQL lite 中有两个表。

tbl_检查点 _id |父 ID |标题 |已检查 |数量

tbl_checkpoint_answers _id |父 ID |回答 |注意 |已检查

游标明智,因为两者都有相同数量的列,数组当然是: 0 = _id 1 = 父代号 2 = 标题/答案 3 = is_checked/注意 4 = 数量/已检查 (/之前是tbl_checkpoints,之后是tbl_checkpoint_answers)

一个检查点可以有一个数量。假设数量是 5。 那么一个检查点需要检查5次。例如,在房子上,检查点是; “砖 block 完好吗” 砖 1:是的 砖2:是的 砖3:没有 砖 4:否 砖5:是的

这些答案属于 table_checkpoint_answers 表。 一对多关系。 一个检查点可以有多个答案。

在代码方面,我正在使用显示表单的 Activity 来填写检查点。 在位于选项卡布局中的表单中。

我在 onCreate() 中有这样的设置

_checkPointCursor = _dbHelper.fetchAllCheckPoints(_parentId);

//I know startManagingCursor() is deprecated. I don't know how to use the new version yet.
startManagingCursor(_checkpointCursor); 

_cursor.moveToPosition(_position);
_answerCursor = _dbHelper.fetchFirstCheckPointAnswer(_checkPointCursor.getInt(0)); 
//getInt(0) returns 1 which is what I expect. (debugged)

这里发生的是我有一个游标,其中包含所有检查点,其中 parentid 等于给定的数字。在这种情况下为 1(已测试。强制为 1,我确信查询会给出结果。

我用于 fetchAllCheckPoints(_parentId) 的查询;是:

SELECT * FROM tbl_checkpoints WHERE parent_id = parentId // parentId = 1

这给了我 5 个结果。这在程序中也是如此,到目前为止它运行良好。 根据调试器,_checkpointCursor mCount 为 5。

下一个查询; fetchFirstCheckPointAnswer();是:

    SELECT * FROM tbl_checkpointAnswers WHERE parent_id = parentId ORDER BY _id ASC LIMIT 0,1
   //primary key of the selected _checkPointCursor() which is 1. so parentId = 1. 
   //Things going well so far..

在这个查询被触发后(我已经使用 2 种软件进行了检查,我得到了 1 个结果。这正是我想要的。

光标现在是 mCount = -1。 为什么?为什么 mCount 在 _answerCursor -1 上? 我确信我从查询中得到了结果。

程序稍后在我使用时崩溃:

textView.setText(_answerCursor.getString(2));

堆栈跟踪:

10-19 14:10:04.651: ERROR/AndroidRuntime(12997): FATAL EXCEPTION: main10-19 14:10:04.651: ERROR/AndroidRuntime(12997): java.lang.RuntimeException: Unable to resume activity {com.wvds.activities/com.wvds.activities.TabbedCheckPointActivity}: java.lang.RuntimeException: Unable to resume activity {com.wvds.activities/com.wvds.activities.CheckPointFormActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 110-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.os.Handler.dispatchMessage(Handler.java:99)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.os.Looper.loop(Looper.java:123)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.main(ActivityThread.java:4627)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at java.lang.reflect.Method.invokeNative(Native Method)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at java.lang.reflect.Method.invoke(Method.java:521)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at dalvik.system.NativeStart.main(Native Method)10-19 14:10:04.651: ERROR/AndroidRuntime(12997): Caused by: java.lang.RuntimeException: Unable to resume activity {com.wvds.activities/com.wvds.activities.CheckPointFormActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 110-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityGroup.onResume(ActivityGroup.java:58)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.Activity.performResume(Activity.java:3823)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     ... 12 more10-19 14:10:04.651: ERROR/AndroidRuntime(12997): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 110-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at com.wvds.activities.CheckPointFormActivity.loadView(CheckPointFormActivity.java:289)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at com.wvds.activities.CheckPointFormActivity.onResume(CheckPointFormActivity.java:126)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.Activity.performResume(Activity.java:3823)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)10-19 14:10:04.651: ERROR/AndroidRuntime(12997):     ... 18 more

希望有人能帮我解决这个问题。我期待阅读您的答案/帮助。

提前致谢。

顺便说一句,以防我有拼写错误,在实际代码中没有拼写错误。并不是说阻止它运行。 由于我的情况。我无法在我的编程电脑上使用互联网。所以我使用互联网 pc 来写这个。

最佳答案

在获取数据之前添加cursor.movetofirst();

关于Android 光标索引超出范围,原因不明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7821256/

相关文章:

Android Activity ,与模板相同的布局注入(inject)不同的内容布局

android - viewModelScope 未取消

android - cursor.getType() 和 CursorIndexOutOfBoundsException 异常

sql - 递归 PostgreSQL 函数失败并显示“游标已在使用”消息

android - 如何使用 asmack android 从 Openfire 检索一对一的聊天记录

android - onRequestPermissionsResult() 未在 fragment 中调用,尝试了我能找到的所有解决方案

sql - 在 SQL 中,如何选择已修改的列?

SQL 当 where x = y 不返回任何行时,使用 x = z

python - 将 @var := a. 属性与 django 原始 sql 一起使用

android - 游标返回不正确的值