Activity not properly managing cursor.

2019-08-18 09:35发布

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.

2条回答
仙女界的扛把子
2楼-- · 2019-08-18 10:06

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

查看更多
男人必须洒脱
3楼-- · 2019-08-18 10:14

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.

查看更多
登录 后发表回答