12

My code is :

public class EventDataSQLHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "my.db";
}

public class Test extends Activity {
    EventDataSQLHelper eventsData;

    @Override
    protected void onDestroy() {
        System.out.println("onDestroy");
  
        close();
        if (db!=null){
       
        db.close();
        
    }
    super.onDestroy();
}

public void close() {
    eventsData.close();
}

I have closed the db here.. In all my activities except this its working(no exception is shown).. Here when i press back button from this Test activity the below exception is thrown

02-14 15:59:34.642: ERROR/Database(535): close() was never explicitly called on database '/data/data/com.tesy.connect/databases/test.db' 
02-14 15:59:34.642: ERROR/Database(535): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-14 15:59:34.642: ERROR/Database(535):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
02-14 15:59:34.642: ERROR/Database(535):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
02-14 15:59:34.642: ERROR/Database(535):     at com.connect.Test.getEvents(Connect_EY.java:755)
02-14 15:59:34.642: ERROR/Database(535):     at com.connect.Test.onCreate(Connect_EY.java:662)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-14 15:59:34.642: ERROR/Database(535):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-14 15:59:34.642: ERROR/Database(535):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 15:59:34.642: ERROR/Database(535):     at android.os.Looper.loop(Looper.java:123)

The exception is thrown on this getEvents line :

public class Test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pageview);

    startRefreshActivity();
    try { /* If No Record Exists Create a New One */
    eventsData = new EventDataSQLHelper(this);
    Cursor cursor = getEvents();
    startManagingCursor(cursor);

    } 
}

private Cursor getEvents() {
    try {
        db = eventsData.getReadableDatabase();
        Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null,
            null, null, null, null);
        startManagingCursor(cursor);
        return cursor;
    } catch (Exception ex) {
        System.out.println("Exception Occured : " + ex.toString());
        return null;
    }
}

Exception :

02-14 15:59:34.642: ERROR/Database(535):     at com.testconnect.Test.getEvents(Test.java:755)
02-14 15:59:34.642: ERROR/Database(535):     at com.testconnect.Test.onCreate(Test.java:662)
4
  • try closing the cursor at onStop(); try this if(db!=null){ db = eventsData.getReadableDatabase(); }
    – Vamsi
    Feb 14, 2011 at 12:00
  • @Vamsi I have tried that but null pointer exception is thrown there Feb 14, 2011 at 12:16
  • Have you implemented onCreate() and onUpgrade() for the SQLiteOpenHelper class?
    – Vamsi
    Feb 14, 2011 at 12:24
  • @Vamsi I have implemented @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { and @Override public void onCreate(SQLiteDatabase db) { in my calss public class EventDataSQLHelper extends SQLiteOpenHelper { Feb 14, 2011 at 12:30

3 Answers 3

6

I resolved the exception. I was calling

 db = eventsData.getReadableDatabase(); 

twice thats why the exception is thrown

1

Did you try:

     Cursor cursor = getEvents();
     startManagingCursor(cursor);
     cursor.close();

?

2
  • I have done this.. but still showing the exception. its shown in the line Cursor cursor = getEvents(); inside the getEvents method db = eventsData.getReadableDatabase(); Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null, null, null, null, null); In the Logcat the exception is thrown in these line Feb 14, 2011 at 12:03
  • Im also facing this Application did not close the cursor or database object that was opened here : Problem.. How to reslove that ..Pls Help
    – jennifer
    Feb 15, 2011 at 5:43
1

Did you try After pass return value close cursor like cursor.close

  private Cursor getEvents() {
     try {
            db = eventsData.getReadableDatabase();
            Cursor cursor = db.query(EventDataSQLHelper.TABLE, null, null,
             null, null, null, null);
            startManagingCursor(cursor);
            cursor.close();
            return cursor;

         } catch (Exception ex) {
              System.out.println("Exception Occured : " + ex.toString());
              return null;
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.