Android "attempted To Access A Cursor After It Has Been Closed"
Solution 1:
Android “attempted to access a cursor after it has been closed”
While you are working / accessing to data in Cursor
, it needs to be opened!
Generally is recommended to close any datasources and cursors in Activity's life-cycle method either onStop()
or onDestroy()
method.
Basic example:
public void onDestroy() {
super.onDestroy();
if (cursor != null) {
c.close();
}
if (db != null) {
db.close();
}
}
Solution 2:
Since I have not enough reputation to comment...
I must add the fact that even though the correct answer said that you could close your cursor onDestroy(), this is not something that you should do.
onDestroy method does not guarantee that Cursor.close() will be called for the fact that android itself does not guarantee that onDestroy would eventually be called.
From documentation:
onDestroy() = The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
Post a Comment for "Android "attempted To Access A Cursor After It Has Been Closed""