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


一般用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM