cottychiu

导航

Attempted to access a cursor after it has been closed 的问题

如果遇到这个错误:
Attempted to access a cursor
after it has been closed.


Application did not close the cursor or database
object that was
opened here 。

原因是:

You should not be getting that message if you
close the Cursor in onStop() or onDestroy().
Please try that again. Or, call
startManagingCursor() after you get the Cursor from your query,
and Android
will close the Cursor on its own.

也就说,你用了
startManagingCursor(c)或者
managedQuery(uri, null, null, null,
null);

这个2个方法里面在操作数据库的时候,当获取完数据了,会自动关闭游标,那么你在不知道的情况下你再次关闭游标,所以造成崩溃。

解决方案:
采用getContentResolver;
比如:
ContentResolver
resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new
String[]{"_id"}, null, null, null);

posted on 2013-08-19 16:36  cottychiu  阅读(919)  评论(0)    收藏  举报