Can someone explain to me this `StaleDataException`

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:57:39

问题


Can someone explain to me this StaleDataException

07-11 19:58:23.298 E/AndroidRuntime( 1044): Uncaught handler: thread main exiting due to uncaught exception
07-11 19:58:23.368 E/AndroidRuntime( 1044): android.database.StaleDataException: Access closed cursor
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
07-11 19:58:23.368 E/AndroidRuntime( 1044): at android.database.CursorWrapper.getInt(CursorWrapper.java:128)

When and how do we need to assure a requiry on the cursor, and why fails with this Exception?


回答1:


You are trying to retrieve information from a Cursor that has already been closed. You must verify whether the cursor is closed or not by using the isClosed method.




回答2:


You can't close the cursor until CursorAdapter is no longer needed. So you can close it in onDestroy() method:

@Override
public void onDestroy() {
 super.onDestroy();

     //Close the cursor
     cursor.close();
     //Close the database
     database.close();
    }



回答3:


In my case, I was closing the cursor in the onStop() method. It turned out that the rotation of the screen was causing this code to run and hence give the StaleDataException.




回答4:


Use Activity.getContentResolver.query() instead of Activity.managedQuery(). Because managedQuery() is deprecated. It works for me.



来源:https://stackoverflow.com/questions/3232002/can-someone-explain-to-me-this-staledataexception

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