应用程序未关闭此处打开的游标或数据库对象:

时间:2011-02-14 10:41:12

标签: android sqlite cursor

我的代码是:

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();
}

我已经在这里关闭了数据库..在我的所有活动中除了它的工作(没有显示异常)..这里当我按下此测试活动的后退按钮时,抛出以下异常

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)

此getEvents行抛出异常:

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;
}

}

例外:

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)

3 个答案:

答案 0 :(得分:6)

我解决了这个异常。我在打电话

 db = eventsData.getReadableDatabase(); 

两次,这就是抛出异常的原因

答案 1 :(得分:1)

你有没有尝试过:

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

答案 2 :(得分:1)

您是否尝试过传递返回值关闭光标,如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;
      }