本文介绍了没有事先的close()的Andr​​oid光标敲定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个列表视图。我得到我的数据从一个SQLiteDatabase的查询。当我从数据库中的数据我得到这个错误:

In my application I have a listview. I get my data with a query from a SQLiteDatabase. When I get the data from the db I get this error:

当我从线20至21它发生。

It occurs when I go from line 20 to 21.

我试过葱50,但没有结果,将cursor.deactivate()和cursor.close()。任何人都知道,为什么我得到这个错误,怎么解决呢?感谢:)

I tried placing cursor.deactivate() and cursor.close() on regel 50. But with no result. Anyone knows why I get this error and how to solve it? Thanks :)

推荐答案

您必须将数据库之前关闭游标。把你的code在try / catch块和finally块,关闭游标,然后关闭数据库:

You have to close the cursor before the database. Put your code in a try / catch block and in a finally block, close the cursor and then close the database:

try {
    db = ...
} catch(Exception ex) {
    // Log the exception's message or whatever you like
} finally {
    try {
      if( cursor != null && !cursor.isClosed )
        cursor.close();
       if( db.isOpen() )
        db.close();
    } catch(Exception ex) {}
}

关闭顺序是相当重要的,而这样做的IO与DB或内容提供商。欲了解更多信息,请参阅this链接

Closing sequence matters a lot while doing IO with DB or Content Providers.For more information refer this link

这篇关于没有事先的close()的Andr​​oid光标敲定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:52