光标在没有事先 close() Android 的情况下完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10434378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:44:13  来源:igfitidea点击:

Cursor finalized without prior close() Android

androidsqlitesqliteopenhelper

提问by Timon Devos

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: error

在我的应用程序中,我有一个列表视图。我通过 SQLiteDatabase 中的查询获取我的数据。当我从数据库中获取数据时,出现此错误: 错误

It occurs when I go from line 20 to 21. occur

它发生在我从第 20 行到 21 行时。 发生

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 :)

我尝试将 cursor.deactivate() 和 cursor.close() 放在 regel 50 上。但没有结果。任何人都知道为什么我会收到此错误以及如何解决它?谢谢 :)

回答by Ali Behzadian Nejad

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

您必须在数据库之前关闭游标。将您的代码放在一个try/catch块中,然后在一个finally块中关闭游标,然后关闭数据库:

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) {}
}

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

在与 DB 或 Content Provider 进行 IO 时,关闭顺序很重要。有关更多信息,请参阅此链接

回答by cV2

to find such problems just enable StrictMode for Debug Version like that:

要找到此类问题,只需为调试版本启用 StrictMode,如下所示:

public void onCreate() {
     if (DEVELOPER_MODE) {
         StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                 .detectDiskReads()
                 .detectDiskWrites()
                 .detectNetwork()   // or .detectAll() for all detectable problems
                 .penaltyLog()
                 .build());
         StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                 .detectLeakedSqlLiteObjects()
                 .detectLeakedClosableObjects()
                 .penaltyLog()
                 .penaltyDeath()
                 .build());
     }
     super.onCreate();
 }

more information @ http://developer.android.com/reference/android/os/StrictMode.html

更多信息@ http://developer.android.com/reference/android/os/StrictMode.html

all the best,

祝一切顺利,

回答by Vincy

Always, remember to close the cursor by calling cursor.close() before closing the database. That should fix your problem.

始终记住在关闭数据库之前通过调用 cursor.close() 来关闭游标。那应该可以解决您的问题。

回答by Akhil

let the activity manage the cursor lifecycly by using startManagingCursor(c)and it will be fine.

让活动通过使用来管理游标生命周期,startManagingCursor(c)就可以了。