Techy
Techy

Reputation: 2654

Checking whether cursor object is closed or not

I am new to android.Is there any statements are available for checking whether cursor object and database is closed or not. I have two activities A & B.when i perform certain actions in A,it will go to B.Database object and cursor object of A are closed in the Activity A.when i press the back button when i am in B,the activity gets force closed.I have understood this error is due to closing of my cursor object.Is there any way to reopen the cursor & database object or can we check whether both are closed or not.if so,please help me.

my logcat view is present below:

10-25 16:02:15.152: ERROR/AndroidRuntime(458): Caused by:java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM tbl_sec) 
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:255)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:188)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at com.nagainfo.firstAp.showTime.onResume(showTime.java:84)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.app.Activity.performResume(Activity.java:3823)
10-25 16:02:15.152: ERROR/AndroidRuntime(458):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)

Upvotes: 1

Views: 2260

Answers (2)

androidExplorer
androidExplorer

Reputation: 136

For closing the database instance, do like this before exiting the activity:

if(db.isOpen()){
    db.close();
}

For checking whether Cursor is available or not, do like this:

if (cursor1.isClosed())
    //Do something here

Upvotes: 1

Alexander
Alexander

Reputation: 48272

Try opening your Cursor in Activity A's onResume() or onStart() instead of onCreate().

Upvotes: 1

Related Questions