就我而言,我将ContentProvider与CursorAdapter一起使用,内容提供程序具有查询方法和bulkInsert。如果bulkInsert在查询方法之前运行,我会得到异常:
java.lang.IllegalStateException: database /data/data/com.example/databases/testdb (conn#0) already closed
我认为这是因为在bulkInsert方法中:
public int bulkInsert(Uri uri, ContentValues[] insertValuesArray) {
switch (sUriMatcher.match(uri)) {
case ROUTE_ENTRIES:
SQLiteDatabase localSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
localSQLiteDatabase.beginTransaction();
localSQLiteDatabase.setTransactionSuccessful();
// do the work
// Ends the transaction and closes the current db instances
localSQLiteDatabase.endTransaction();
localSQLiteDatabase.close();
}
}
如您所见,在bulkInsert方法中调用了localSQLiteDatabase.close()。当我跟踪谷歌的threadsample时,我这样做了。链接:https://code.google.com/p/android-source-browsing/source/browse/samples/training/threadsample/src/com/example/android/threadsample/DataProvider.java?repo=platform--development&name=tools_r22&r=2c063c889aa816e0de91bf17fdc0c78f48d5e2d0
任何人都可以建议我是否应该关闭bulkInsert中的数据库?如果我不关闭,有没有泄漏?
非常感谢!
欢呼声,
马丁