如何避免DatabaseObjectNotClosedException

时间:2011-01-19 11:52:02

标签: android

我们如何避免这种异常?

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): Releasing statement in a finalizer.    Please ensure that you explicitly call close() on your cursor: SELECT * FROM test

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:62)

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80)

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):     at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46)

4 个答案:

答案 0 :(得分:5)

我认为出现此错误是因为您没有关闭数据库。始终关闭sqlitehelper类

<强>编辑:

你必须在你的类中实现这样的扩展SQLiteOpenHelper类:

@Override
public synchronized void close() {
    if(db != null){
        db.close();
    super.close();
    }   
}

答案 1 :(得分:3)

好吧,正如它所说,在你的光标上调用close()!

答案 2 :(得分:1)

当我使用游标时出现错误的SQL查询时,我也得到了异常。这可能听起来很愚蠢,但如果有人发现相同的话,这是另一个原因......

答案 3 :(得分:0)

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

 db = eventsData.getReadableDatabase(); 

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