Application did not close the cursor or database object that was opened here

        这两天调试程序的时候,发现LogCat里时不时的会打出"DatabaseObjectNotClosedException"这个异常,异常的详细堆栈信息如下:

 

close() was never explicitly called on database '/data/data/cn.eric.sqlitedbdemo/databases/people.db'
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1827)
    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:541)
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    at cn.eric.sqlitedbdemo.SQLiteDatabaseHelper.query(SQLiteDatabaseHelper.java:61)
    at cn.eric.sqlitedbdemo.SQLiteDatabaseDemo$1$1.run(SQLiteDatabaseDemo.java:53)

 

但是这个异常的抛出并没有让程序崩溃。

    这些异常信息来源于SQLiteDatabase类的finalize方法。从异常的信息"close() was never explicitly called on database ,Application did not close the cursor or database object that was opened here
"可以知道这是由于程序中使用到的游标或SQLiteDatabase对象没有close所导致。也就是说在程序中创建的Cursor对象或者SQLiteDatabase对象,在使用完后没有关闭,而当它们都变成“垃圾"被GC时,就会打出以上的信息。

    @Override
    protected void finalize() {
        if (isOpen()) {
            Log.e(TAG, "close() was never explicitly called on database '" +
                    mPath + "' ", mStackTrace);
            closeClosable();
            onAllReferencesReleased();
        }
    }

 

    所以要想不发生这样的错误,在编码时一定要记住,当你不在使用Cursor或SQLiteDatabase对象时,将它们close(),

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值