代码改变世界

Unable to resume activity : android.database.StaleDataException: Attempted to access a cursor after it has been closed. 异常

2013-05-17 19:21  清炒白菜  阅读(4229)  评论(0编辑  收藏  举报

一般用Cursor后, 就立即close()了,但是在用managedQuery的时候, 却不能主动调用close()方法, 否则在Android 4.0+的系统上, 会发生崩溃

google的文档是这么写的

Warning: Do not call close() on a cursor obtained using this method, because the activity will do that for you at the appropriate time. However, if you call stopManagingCursor(Cursor) on a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call close().

 

也有人说用下面的方法可以避免这个问题

这个问题其实很简单  估计大家在查询数据库的时候用的是这个函数  
viedoCursor = context.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns,null, null, null);
只要换成这样就可以了:
ContentResolver cr = context.getContentResolver();
viedoCursor = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns,null, null, null);
我也是无意间发现有着两种写法的 然后试了一下问题就解决了

 

参考:http://stackoverflow.com/questions/9696868/unable-to-resume-activity-error

http://changyy.pixnet.net/blog/post/31238721-android-%E9%96%8B%E7%99%BC%E7%AD%86%E8%A8%98---unable-to-resume-activity-%3A-android.d