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

  • SEARCH
  • Home
  • 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 1053955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:19:52+00:00 2026-05-16T17:19:52+00:00

I have a SQLiteDatabase data member that I initialize in onCreate and call .close()

  • 0

I have a SQLiteDatabase data member that I initialize in onCreate and call .close() on in onPause(), onStop(), and onDestroy(). It is re-initialized in onResume(). It seems to run just fine but when I looked at the debugger it see this:

08-24 20:23:50.014: ERROR/Database(6767): Leak found
08-24 20:23:50.014: ERROR/Database(6767): java.lang.IllegalStateException: /data/data/com.hh.Timepunch/databases/times_database.db SQLiteDatabase created and never closed
08-24 20:23:50.014: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
08-24 20:23:50.014: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
08-24 20:23:50.014: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761)
08-24 20:23:50.014: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:476)
08-24 20:23:50.014: ERROR/Database(6767):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
08-24 20:23:50.014: ERROR/Database(6767):     at com.hh.Timepunch.TimeSheetActivity.onCreate(TimeSheetActivity.java:72)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-24 20:23:50.014: ERROR/Database(6767):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 20:23:50.014: ERROR/Database(6767):     at android.os.Looper.loop(Looper.java:123)
08-24 20:23:50.014: ERROR/Database(6767):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-24 20:23:50.014: ERROR/Database(6767):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 20:23:50.014: ERROR/Database(6767):     at java.lang.reflect.Method.invoke(Method.java:521)
08-24 20:23:50.014: ERROR/Database(6767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-24 20:23:50.014: ERROR/Database(6767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-24 20:23:50.014: ERROR/Database(6767):     at dalvik.system.NativeStart.main(Native Method)
08-24 20:23:50.054: ERROR/Database(6767): Leak found
08-24 20:23:50.054: ERROR/Database(6767): java.lang.IllegalStateException: /data/data/com.hh.Timepunch/databases/times_database.db SQLiteDatabase created and never closed
08-24 20:23:50.054: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1695)
08-24 20:23:50.054: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:739)
08-24 20:23:50.054: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:761)
08-24 20:23:50.054: ERROR/Database(6767):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:754)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:476)
08-24 20:23:50.054: ERROR/Database(6767):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
08-24 20:23:50.054: ERROR/Database(6767):     at com.hh.Timepunch.TimepunchActivity.onCreate(TimepunchActivity.java:60)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-24 20:23:50.054: ERROR/Database(6767):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 20:23:50.054: ERROR/Database(6767):     at android.os.Looper.loop(Looper.java:123)
08-24 20:23:50.054: ERROR/Database(6767):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-24 20:23:50.054: ERROR/Database(6767):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 20:23:50.054: ERROR/Database(6767):     at java.lang.reflect.Method.invoke(Method.java:521)
08-24 20:23:50.054: ERROR/Database(6767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-24 20:23:50.054: ERROR/Database(6767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-24 20:23:50.054: ERROR/Database(6767):     at dalvik.system.NativeStart.main(Native Method)
08-24 20:23:50.199: ERROR/libs3c2drender(1912): int S3c2DRender::DoG2D(unsigned int, s3c_img*, s3c_rect*, unsigned int, s3c_img*, s3c_rect*, int, int)::S3C_G2D_ROTATOR_18176 fail

One of the places I get this error is when going away from this activity:

public class TimepunchActivity extends Activity {

        Calendar timeNow = Calendar.getInstance();
        SQLiteDatabase timesDatabase;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.timepunch);

            //Create/open and configure database (if necessary)
            timesDatabase = openOrCreateDatabase(
                    "times_database.db",
                    SQLiteDatabase.CREATE_IF_NECESSARY,
                    null);
            timesDatabase.setLocale(Locale.getDefault());
            timesDatabase.setLockingEnabled(false);
            timesDatabase.setVersion(3);

            //lenient is set to true so incrementing minute 59 auto increments hour, etc
            timeNow.setLenient(true);

            //Create table (if necessary)
            final String dbCommand = "CREATE TABLE IF NOT EXISTS Timepunches (punch INTEGER PRIMARY KEY);";
            timesDatabase.execSQL(dbCommand);

            updateLastInPunch();

            Button punchButton = (Button)findViewById(R.id.punch_button);
            punchButton.setHapticFeedbackEnabled(true);
            punchButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //Insert data
                    ContentValues punchValue = new ContentValues();
                    punchValue.put("punch", timeNow.getTimeInMillis());
                    long newPunchID = timesDatabase.insert("Timepunches", null, punchValue);
                    punchValue.clear();

                    //if punch already exists
                    if (newPunchID < 0) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(TimepunchActivity.this);
                        builder.setTitle("Whoops")
                            .setMessage("I've already recorded a punch for this time and day.")
                            .setCancelable(true)
                            .setNeutralButton("Oh", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            })
                            .show();
                    }
                    else {
                    }
                } //end setOnClick()
            }); //end click listener for punchButton
        } //end onCreate()



        @Override
        public void onResume() {
            super.onResume();
            checkFirstTimer();

            //Open and configure database
            timesDatabase = openOrCreateDatabase(
                    "times_database.db",
                    SQLiteDatabase.CREATE_IF_NECESSARY,
                    null);
            timesDatabase.setLocale(Locale.getDefault());
            timesDatabase.setLockingEnabled(false);
            timesDatabase.setVersion(3);


            //Update date object for current time
            timeNow.setTimeInMillis(System.currentTimeMillis());
            timeNow.set(Calendar.SECOND, 0);
            timeNow.set(Calendar.MILLISECOND, 0);
            updateClock((TextView)findViewById(R.id.current_time_textbox), timeNow);

            updateLastInPunch();
        } //end onResume()


        @Override
        public void onPause() {
            super.onPause();
            timesDatabase.close();
        } //end onResume()

        @Override
        public void onStop() {
            super.onStop();
            timesDatabase.close();
        } //end onResume()

        @Override
        public void onDestroy() {
            super.onDestroy();
            timesDatabase.close();
        } //end onResume()



        //Shows last in-punch that has no out-punch
        // if there is none it hides the section
        public void updateLastInPunch() {
            Cursor punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;");

            TextView inPunchLabel = (TextView)findViewById(R.id.in_punch_label_textview);
            TextView inPunchTV = (TextView)findViewById(R.id.in_punch_time_textview);

            //odd count means there is a missing punch (assumed to be latest in punch)
            // therefore show last in punch
            if ((punchCursor.getCount()%2) != 0) {
                punchCursor.moveToLast();
                Calendar lastIn = Calendar.getInstance();
                lastIn.setTimeInMillis(punchCursor.getLong(0));

                updateClock((TextView)findViewById(R.id.in_punch_time_textview), lastIn);

                inPunchLabel.setVisibility(View.VISIBLE);
                inPunchLabel.setAnimation(fadeInAnimation);
                inPunchLabel.startAnimation(fadeInAnimation);

                inPunchTV.setVisibility(View.VISIBLE);
                inPunchTV.setAnimation(fadeInAnimation);
                inPunchTV.startAnimation(fadeInAnimation);
            }
            else {
                //only fade when "turning off" last punch display
                // (don't fade away when returning to screen)
                if (inPunchLabel.getVisibility() == View.VISIBLE) {
                    inPunchLabel.setAnimation(fadeOutAnimation);
                    inPunchLabel.startAnimation(fadeOutAnimation);
                    inPunchLabel.setVisibility(View.INVISIBLE);

                    inPunchTV.setAnimation(fadeOutAnimation);
                    inPunchTV.startAnimation(fadeOutAnimation);
                    inPunchTV.setVisibility(View.INVISIBLE);
                }
            }
            punchCursor.close();
        } //end getLastInPunch()
    } //end TimepunchActivity

The odd thing is, though, is that this error isn’t persistent.

Do I have to do more then call .close()?

  • 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-16T17:19:53+00:00Added an answer on May 16, 2026 at 5:19 pm

    The problem is that you’re creating/opening it in both onCreate and then onResume, so you’re doing it twice. Likewise, you’re closing it twice, so it’ll presumably crash.

    In general – why don’t you use SQLiteOpenHelper? This will make managing your database a lot easier.

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

Sidebar

Related Questions

I have a datalogging application (c#/.net) that logs data to a SQLite database. This
I have an app that must get data from the Sqlite database in order
My application manages all its data in a a SQLiteDatabase that is accessed by
I have an sqlite database that has a blob column with blob data. I
I have android app that talks to server and syncs some data int SQLite
I have an app that functions properly and does not force close or crash.
I have a ListView that populates data from a DB into a complex-ListView. A
I have a listview which is filled with data form sqlitedatabase. A list-view element
I have two instances of SQLiteDatabase database. And I need to copy data from
I have an app that must open a sqlitedatabase from a .db file on

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.