Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:45:58+00:00 2026-05-25T06:45:58+00:00

I have been trying to understand and solve this problem for the last three

  • 0

I have been trying to understand and solve this problem for the last three days.
I have multiple ListActivities where each one queries the same database. In every activity I close and reopen DB connection in onPause() and onResume() respectively. I populate list adapters with cursors which I also close and reopen:

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

    if(currentCursor != null){
        currentCursor.close();
        currentCursor = null;
    }

    if(mDbHelper != null){
        mDbHelper.close();
        mDbHelper = null;
    }
}

@Override
public void onResume(){
    super.onResume();
    if(mDbHelper == null){
        mDbHelper = new DbHelper(this);
        mDbHelper.open();
    }

    if(currentCursor == null){
        new LoadNewListTask().execute();
    }
}


private class LoadNewListTask extends AsyncTask<String, Void, String> {
    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Loading...");
        dialog.show();
    }
    @Override
    protected String doInBackground(String... arg0) {
        try{
            currentCursor = mDbHelper.fetchNewRows();
        }catch(Exception e){}
        return null;
    }
    @Override
    protected void onPostExecute(String arg){
        if(dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
        if(currentCursor != null && currentCursor.getCount()>0){
            String[] from = new String[]{DbHelper.KEY_ID, sortingColumn};
            int[] to = new int[]{R.id.ai_about_author_btn, R.id.ai_author_name};

            MyAlphabetizedAdapter notes = new MyAlphabetizedAdapter(this, R.layout.author_index_item, currentCursor, from, to, sortingColumn);
            setListAdapter(notes);
        }
    }
}

On the first glance it works good however when I move fast between activities I sometimes get annoying Cursor Exceptions one saying this:

05-01 14:45:05.849: ERROR/AndroidRuntime(1145):
java.lang.IllegalStateException: attempt to acquire a reference on a
close SQLiteClosable
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:31)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:56)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1118)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1092)
05-01 14:45:05.849: ERROR/AndroidRuntime(1145):     at
apt.tutorial.Restaurant.getAll(Restaurant.java:14)

and another one saying this (this happens a LOT on 2.1 devices):

09-03 11:30:19.713: INFO/dalvikvm(2541): Uncaught exception thrown by finalizer (will be discarded):
09-03 11:30:19.713: INFO/dalvikvm(2541): Ljava/lang/IllegalStateException;: Finalizing cursor android.database.sqlite.SQLiteCursor@45b99828 on null that has not been deactivated or closed
09-03 11:30:19.713: INFO/dalvikvm(2541):     at android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596)
09-03 11:30:19.713: INFO/dalvikvm(2541):     at dalvik.system.NativeStart.run(Native Method)

What is more one of my ListActivities implements AlphabetIndexer where I have to pass the cursor. Very strange thing happens after I leave the activity and come back using back button. The list is populated (as expected) however when I try to use fast scrolling I get a null exception on the cursor even thought I recreated it and repopulated the list onResume().

For me it looks like OS cannot handle multiple cursors properly or maybe I miss something here?

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:45:59+00:00Added an answer on May 25, 2026 at 6:45 am

    In general, I’ve had issues with Activities that try to keep multiple cursors alive. Especially when trying to use a managed cursor which I now try to avoid if possible (plus they’re deprecated). Now, what I try to do is open the cursor, populate an array of objects with the data I need, close the cursor and then use an ArrayAdapter to populate the ListView. If you DO need to keep the cursor alive, and you should only need to if other activities on your stack are changing data, use a cursor manager or, better yet, the newer CursorLoader. When doing this, don’t close the cursor, as your activity will manage the lifecycle of the cursor for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to solve this problem for three days now, it's really
I've been trying to solve this problem for a couple of hours but I
I have been trying to understand of this following C-program: #include <stdio.h> int arr[]
I have been trying it for few days and I do not understand what
I have been trying to understand a bit more about the wider picture of
I have been trying to understand the way ActionScript's events are implemented, but I'm
I have been trying to get around this error for a day now and
I have been trying to understand why it will not work for me. Im
I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet .
I understand there are many threads like mine but I have been trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.