活动未正确管理光标。

发布于 2024-11-26 23:49:22 字数 1595 浏览 5 评论 0原文

我有一个活动可以填充列表并通过光标执行其他一些操作。我从一个方法中获取光标,该方法根据标准的 android SQLite 查询将其返回到我的数据库。该方法在我的 SQLHelper 类中定义。该方法打开数据库、查询、关闭数据库并返回游标。在活动中收集光标后,我调用 startManagingCursor(cursor);

一切正常。

当我启动子活动然后返回第一个活动时,就会出现问题。我得到以下输出的强制关闭:

07-28 18:11:04.674: ERROR/AndroidRuntime(224): java.lang.RuntimeException: Unable to resume activity {blabla}: java.lang.IllegalStateException: mQuery SELECT * FROM vehicles WHERE _id=? 1 
...
07-28 18:11:04.674: ERROR/AndroidRuntime(224): Caused by: java.lang.IllegalStateException: mQuery SELECT * FROM vehicles WHERE _id=? 1 
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:162)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:536)

...
07-28 18:11:04.674: ERROR/AndroidRuntime(224): Caused by: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence: handle 0x0
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:178)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:153)

问题显然是在它尝试重新查询游标时。如果我在启动意图之前手动关闭光标,那么它可以正常工作。但随后我需要手动重新查询它,这不是 startManagingCursor() 的全部意义吗?我在活动中有两个游标,它们都是以相同的方式创建的,我在这两个游标上调用开始管理游标,这两个游标都会导致恢复时崩溃。

我还需要做些什么才能让我的活动正确管理我的光标吗? 谢谢。

I have an activity that populates a list and does some other things from a cursor. I get the cursor from a method that returns it based on a standard android SQLite query to my database. The method is defined in my SQLHelper class. The method opens the database, queries, closed the database, and returns the cursor. After the cursor has been collected in the activity, I call startManagingCursor(cursor);

All that works fine.

The problem happens when I launch a sub activity and then come back to the first one. I get a force close with the following output:

07-28 18:11:04.674: ERROR/AndroidRuntime(224): java.lang.RuntimeException: Unable to resume activity {blabla}: java.lang.IllegalStateException: mQuery SELECT * FROM vehicles WHERE _id=? 1 
...
07-28 18:11:04.674: ERROR/AndroidRuntime(224): Caused by: java.lang.IllegalStateException: mQuery SELECT * FROM vehicles WHERE _id=? 1 
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:162)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:536)

...
07-28 18:11:04.674: ERROR/AndroidRuntime(224): Caused by: android.database.sqlite.SQLiteMisuseException: library routine called out of sequence: handle 0x0
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:178)
07-28 18:11:04.674: ERROR/AndroidRuntime(224):     at android.database.sqlite.SQLiteQuery.requery(SQLiteQuery.java:153)

The problem is apparently when it is trying to re-query the cursor. If I close the cursor manually before I launch the intent, then it works fine. But then I need to re-query it manually and isn't that the entire point of startManagingCursor()? I have two cursors in the activity that are both created in the same way and I call start managing cursor on both, both cause a crash on resume.

Is there something else I need to do to get my activity to properly manage my cursors?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-12-03 23:49:22

尝试在 onPause() 中关闭光标 (cursor.close())。如果您使用的是 SimpleCursorAdapter,您还应该将光标与适配器分离。

如果您愿意,您可以在 onResume() 方法中重新查询光标。

Try closing the cursor in the onPause() (cursor.close()). If you are using a SimpleCursorAdapter you should also detach the cursor from the adapter.

You can requery for the cursor in the onResume() method if you wish.

谁的新欢旧爱 2024-12-03 23:49:22

好吧,事实证明 Cursor.requery() 已被贬值,每次尝试调用它时我都会遇到相同的错误。 startManagingCursor() 正在调用 requery,正如我认为应该的那样,这反过来又导致了我的问题...我不确定为什么 requery 在这种情况下会导致错误,我之前已经成功使用过它。

我最终重新安排了一些代码,并将光标 = SQLHelper.getwhatevercursor() 代码放入 onResume() 而不是 onCreate 中,因为 onResume 是在 onCreate 之后立即调用的。根据 Nikola 的建议,我也在 onPause() 中关闭游标。

这似乎现在有效......

Well, it turns out that Cursor.requery() is depreciated and I was getting the same error every time I tried to call it. startManagingCursor() was calling requery, as I suppose it should, and that in turn was causing my problems there... I'm not sure why requery is causing an error in this instance, I've used it successfully before.

I ended up rearranging some code, and putting my cursor = SQLHelper.getwhatevercursor() code into onResume() instead of onCreate, as onResume is called right after onCreate anyway. As recommended by Nikola I am also closing the cursors in onPause().

This seems to be working now...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文