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 4327702
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:34:33+00:00 2026-05-21T09:34:33+00:00

Hey. I’m trying to return a cursor object to my activity where its gonna

  • 0

Hey. I’m trying to return a cursor object to my activity where its gonna be used in a SimpleCursorAdapter, but Im having a close() was never explicity called error. I cant find any solution for this error, and I’m about to think that its a non-solution error, LOL.

Think with me.

The error says:

close() was never explicitly called on
database
‘/data/data/com.example.myapp/databases/myDB.db’

And has a warning too:

Releasing statement in a finalizer.
Please ensure that you explicitly call
close() on your cursor: SELECT * FROM
contact_data ORDER BY duration desc

android.database.sqlite.DatabaseObjectNotClosedException:
Application did not close the cursor or database object that was opened here

The error is in this method, that is in the class DataHandlerDB (deals with database)

public static Cursor selectTopCalls(Context ctx) {

        OpenHelper helper = new OpenHelper(ctx);
        SQLiteDatabase db = helper.getWritableDatabase(); // error is here

        Cursor cursor = db.query(TABLE_NAME_2, null, null, null, null, null,
                "duration desc");

        return cursor;
    }

This method is used on my activity, by this following method:

public void setBasicContent() {

    listview = (ListView) findViewById(R.id.list_view); 

    Log.i(LOG_TAG, "listview " + listview);

    Cursor c = DataHandlerDB.selectTopCalls(this); // here I use the method
    startManagingCursor(c);

    adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[] {               
            DataHandlerDB.CONTACT_NAME_COL,
            DataHandlerDB.CONTACT_NUMBER_COL,
            DataHandlerDB.CONTACT_DURATION_COL,
            DataHandlerDB.CONTACT_DATE_COL }, new int[] {
            R.id.contact_name, R.id.phone_number, R.id.duration, R.id.date });

    Log.i(LOG_TAG, "before setAdapter");

    Toast.makeText(this, "Before setAdapter", Toast.LENGTH_SHORT).show();

    listview.setAdapter(adapter);


}

I try to close the cursor and database inside this method, but when I do that, the error is not fixed, and it doesnt print my list.

I tried to close it on the selectValues() method, but when doing that, it says, trying to re-open cursor already closed (something like that).

I also tried to close the cursor and database in onDestroy(), onStop() but it didnt worked.

Thats why I thought there were no soluytion for that. What am I supose to do?

The class DataHandlerDB.java, has a createDB() method:

public static SQLiteDatabase createDB(Context ctx) {
        OpenHelper helper = new OpenHelper(ctx);
        SQLiteDatabase db = helper.getWritableDatabase();
        helper.onOpen(db);
        db.close();
        return db;
    }

And an Inner Class called OpenHelper(extends SQLiteOpenHelper)

public static class OpenHelper extends SQLiteOpenHelper {

        private final Context mContext;

        OpenHelper(Context context) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.mContext = context;

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            String[] sql = mContext.getString(
                    R.string.MyString_OnCreate).split("\n");

            db.beginTransaction();

            try {
                execMultipleSQL(db, sql);
                db.setTransactionSuccessful();
            } catch (SQLException e) {

                Log.e("Error creating tables and debug data", e.toString());
                throw e;

            } finally {
                db.endTransaction();

            }
        }

        private void execMultipleSQL(SQLiteDatabase db, String[] sql) {

            for (String s : sql) {

                if (s.trim().length() > 0) {

                    db.execSQL(s);
                }
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            /*
             * Log.w("My Database",
             * "Upgrading database, this will drop tables and recreate.");
             * db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);
             */
        }

        @Override
        public void onOpen(SQLiteDatabase db) {

            super.onOpen(db);
        }

    }

So anyone can help me to fix this problem?? Thanks!

  • 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-21T09:34:34+00:00Added an answer on May 21, 2026 at 9:34 am

    Instead of creating a new OpenHelper in each of your static methods, instantialize your DataHandlerDB with the current context and have the class hold a variable populated by getWritableDatabase. You are creating multiple cursor objects created by different SQLiteOpenHelper objects and Android doesn’t like doing this.

    Check this out for additional info: http://www.ragtag.info/2011/feb/1/database-pitfalls/

    Here’s how it looks to me like you’re doing things…

    public class DataHandlerDB{
    
        public static SQLiteDatabase createDB(Context ctx) {
            OpenHelper helper = new OpenHelper(ctx);
            SQLiteDatabase db = helper.getWritableDatabase();
            ...
            return db;
        }
    
        public static Cursor selectTopCalls(Context ctx) {
            OpenHelper helper = new OpenHelper(ctx);
            SQLiteDatabase db = helper.getWritableDatabase(); // error is here
            ...
            return c;
        }
    
    }
    

    This results in multiple concurrent SQLiteOpenHelper objects and multiple SQLiteDatabase objects and the locking situation you currently have.

    Instead of doing multiple Static calls, make a DataHandler class that you instantialize with the consistent context and then make normal calls (instead of static ones):

    public class DataHandlerDB{
        OpenHelper _helper;
        SQLiteDatabse _db;
    
        public DataHandlerDB( Context ctx ){
            _helper = new OpenHelper(ctx);
            _db = _helper.getWritableDatabase();
        }
    
        public SQLiteDatabase createDB() {
            ...
            return db;
        }
    
        public Cursor selectTopCalls() {
            ...
            return c;
        }
    
    }
    
    public void setBasicContent() {
    
        ...
    
        DataHandlerDB handler = new DataHandlerDB( this );
        Cursor c = handler.selectValues();  //.selectTopCalls()?
    
        ...
    }
    

    When you create this object, it will persist 1 OpenHelper and 1 SQLiteDatabase. This should alleviate the problems with SQLite wanting the database closed before it can access it.

    Don’t forget to close the DB in the onDestroy method of your activity.

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

Sidebar

Related Questions

Hey having some trouble trying to maintain transparency on a png when i create
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey, I'm trying to make an Address book for my site and I was
Hey Forum, So i'm trying to find out how to use the source files
Hey all, i am trying to create a report to show how much is
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, I've been developing an application in the windows console with Java, and want

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.