【问题标题】:Android: Finalizing a Cursor that has not been deactivated or closedAndroid:完成尚未停用或关闭的光标
【发布时间】:2011-08-01 09:14:41
【问题描述】:

我有一个需要使用 SQLite(Android 数据库)的应用程序。

我为我的模型和数据助手创建了一个类似 JPA 的结构。 问题是当我在 helper.search(int id) 上调用另一个 aHelper.search(int id) 时,Finalizing a Cursor 错误日志。

return Child(cursor.getInt(0), new ParentHelper.search(cursor.getInt(1)));

我的类似 JPA 的模型和数据助手的结构是这样的。

class Parent {
   int id;
   String name
   // constructor with field-parameter
   // getters and setters
}

class Child {
   int id;
   Parent parent;
   // constructor with field-parameter
   // getters and setters
}

class ParentHelper {
   // necessary SQLiteImpelemntations
   Parent search(int id) {
      // new Cursor implementation
      cursor.moveToFirst();
      return new Parent(cursor.getInt(0), cursor.getString(1));
   }
}

class ChildHelper {

   ParentHelper parentHelper;

   void close() {
      parenHelper.close()
      SQLiteDatabase.close();
      SQLiteOpenHelper.close();
   }

   ArrayList searchAll() {
      // new Cursor implementation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      ArrayList.add(child)
      cursor.close();
   }

   // necessary SQLiteImpelemntations
   Child search(int id) {
      // new Cursor implemenation
      cursor.moveToFirst();
      Child child = new Child(cursor.getInt(0), parentHelper.search(cursor.getInt(1)))
      cursor.close();
      return child;
   }   
}

【问题讨论】:

  • 已更新为代码。以分层方式运行多个光标(如在 JPA 中)时卡住。 Child.search().Cursor 与 Parent.search().Cursor.

标签: android sqlite cursor


【解决方案1】:

在返回值之前尝试关闭光标。

class ParentHelper {
// necessary SQLiteImpelemntations
Parent search(int id) {
    // new Cursor implementation
    cursor.moveToFirst();
    Parent p = new Parent(cursor.getInt(0), cursor.getString(1));
    cursor.close();
    return p;
    }
}

为您的 ChildHelper 做同样的事情。

【讨论】:

  • 错误已更改为“close() 从未在数据库上显式调用”。
  • 我不知道你在哪里得到这个错误,但你应该尝试在某个时候关闭你的 SQLiteDatabase 变量 (SQLiteDatabase db; db.close();)
  • 看起来很奇怪。但是当我试图关闭另一个助手的实例时。它没有改变任何事情。那我该如何解决呢?
  • 如果没有更多信息,我不知道您必须在哪里关闭数据库。看看这些问题,它们可能会对你有所帮助:Question1, Question2
  • 关于问题1:根据我的代码,我正在从正在运行的游标中打开另一个游标;问题 2:因为根据某些人的说法,不推荐使用 startManagingCursor [不确定 2.2],我不能使用建议的答案。
猜你喜欢
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多