Android error - close() was never explicitly called on database

微笑、不失礼 提交于 2019-11-26 19:46:39

I think the problem is that you need to close the db when your activity is destroyed. Try adding this to your activity:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (openHelper != null) {
        openHelper.close();
    }
    if (cdh != null) {
        cdh.close();
    }
}

and add this to your CallDataHelper class:

public void close() {
    // NOTE: openHelper must now be a member of CallDataHelper;
    // you currently have it as a local in your constructor
    if (openHelper != null) {
        openHelper.close();
    }
}

EDIT 2: Also change the CallDataHelper constructor to:

// Declare openHelper as a member variable
OpenHelper openHelper = null;

public CallDataHelper(Context context) {
    this.context = context;
    openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}

This should successfully close the database connection that was created by both of your OpenHelper instances.

( EDITED to reflect the fact that you use two OpenHelper instances.)

Literally understand database is not normally closed, the actual because repeated instantiation your database, or connect you have been set up, and you and try to open another connection there will be no exception. So the solution is, make sure that you open only a connection.

I suspect fillList() is called multiple times during a RatedCalls "session". So each time you create a new Cursor instance and call startManagingCursor() on it. Doing so you accumulate a list of Cursors - ArrayList<ManagedCursor> to be precise that is declared in ListActivity. Normally they (cursors that are being managed) will be closed automatically in ListActivity.onDestroy(). Could you confirm your RatedCalls actually passes onDestroy().

Do you override somehow the ListActivity.onDestroy() in RatedCalls. If yes, then do you call super.onDestroy() (you should call it) ?

I don't get the purpose of calling onStop() in RatedCallsContentObserver.onChange() after the fillList();. I believe Activity life-cycle callbacks should be called by OS only. So maybe calling a life-cycle callback manually you somehow prevent from onDestroy() to be called by OS (just a guess)?

UPDATE:

Dan Breslau is right - it's db object that is not closed. Sorry for misleading.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!