Activity not properly managing cursor.

六月ゝ 毕业季﹏ 提交于 2019-12-11 16:16:49

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6863744/activity-not-properly-managing-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!