Android 光标索引出于未知原因越界
我的光标有问题,我知道错误,但不知道如何解决这个问题。 在进入代码模式之前,让我先解释一下我想要实现的想法。
我在 SQL Lite 中有两个表。
表检查点 _id |父 ID |标题 |已检查 |数量
tbl_checkpoint_answers _id |父 ID |答案|注意| is_checked
游标明智,因为两者具有相同数量的列,所以数组当然是: 0 = _id 1 = 父 ID 2 = 标题/答案 3 = 已检查/注释 4 = 数量/已检查 (/之前是tbl_checkpoints,之后是tbl_checkpoint_answers)
一个检查点可以有一个数量。举例来说,数量是 5。 那么一个检查点需要检查5次。例如,在一栋房子上,检查点是: “砖块状况是否完好” 砖1:是的 砖2:是的 砖3:无 砖4:无 砖 5:是
这些答案属于 table_checkpoint_answers 表。 一对多的关系。 一个检查点可以有多个答案。
在代码方面,我正在使用一个显示表单的活动来填写检查点。 在表单中,它驻留在选项卡布局中。
我在 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..
此查询触发后(我使用两种软件进行了检查,我得到了 1 个结果。这正是我想要的。
光标现在显示 mCount = -1。 为什么?为什么 _answerCursor 上的 mCount 为 -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
我希望有人能够帮助我解决这个问题。我期待阅读您的答案/帮助。
提前致谢。
顺便说一句,如果我有拼写错误,在实际代码中没有拼写错误。并不是阻止它运行。 由于我所处的情况,我无法在我的编程电脑上使用互联网。所以我用一台联网电脑来写这篇文章。
I have a problem with my cursor which I know the error of, but hav eNO clue how to solve this.
Let me explain the idea im trying to achieve first before diving into code-mode.
I have two tables in SQL lite.
tbl_checkpoints
_id | parent_id | title | is_checked | quantity
tbl_checkpoint_answers
_id | parent_id | answer | note | is_checked
Cursor wise since both have an equal amount of columns, the array would ofcourse be:
0 = _id
1 = parent_id
2 = title/answer
3 = is_checked/note
4 = quantity/is_checked
(before the / is tbl_checkpoints, after is tbl_checkpoint_answers)
A checkpoint can have a quantity. Let's say for example the quantity is 5.
Then a checkpoint requires to be checked 5 times. On a house for example, the checkpoint would be;
"Is brick in perfect condition"
brick 1: yes
brick 2: yes
brick 3: no
brick 4: no
brick 5: yes
These answers belong in the table_checkpoint_answers table.
One to many relationship.
One checkpoint can have multiple answers.
Code wise, I am using an activity that displays a form, to fill in a checkpoint.
In the form, which resides in a tab layout.
I have a setup like this in 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)
What happens here is I have a cursor that has all checkpoints where parentid is equal to a given number. 1 in this case(tested. Forced it to be 1, which im sure of the query will give results.
The query I use for fetchAllCheckPoints(_parentId); is:
SELECT * FROM tbl_checkpoints WHERE parent_id = parentId // parentId = 1
This gives me 5 results. This is true in the program aswell and it works fine uptil now.
_checkpointCursor mCount is 5 according to the debugger.
The next query; fetchFirstCheckPointAnswer(); is :
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..
After this query has fired (I have checked using 2 kinds of software, I get 1 result. Which is exactly what I want.
The cursor now sais mCount = -1.
Why? why is mCount on _answerCursor -1?
I am sure I get a result from the query.
the program later crashes when I use:
textView.setText(_answerCursor.getString(2));
The stacktrace:
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
I hope someone will be able to help me resolve this issue. I look forward to reading your answers/help.
Thanks in advance.
Btw, In case I have typos, in the actual code there are no typos. Not that prevent it from running.
Due to the situation im in. I cant use internet on my programming pc. So I use an internet pc to write this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在查找数据之前添加 cursor.movetofirst();
Before fatch data you add cursor.movetofirst();