开发者

Android: Finalizing a Cursor that has not been deactivated or closed

I have an application which requires the use of SQLite (Android's database).

I have created a JPA-like structure to my model and datahelper. The problem is when I call another aHelper.search(int id) while on a helper.search(int id), the Finalizing a Cursor error logs.

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

The structure to my JPA-like model and datahelper goes like this.

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 Parent开发者_JAVA百科Helper {
   // 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;
   }   
}


Try closing your Cursor before returning the value.

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;
    }
}

Do the same for your ChildHelper.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜