Ormlite SQLiteCursor:将null上的游标关闭两次或更多次

时间:2014-08-11 14:06:35

标签: android android-sqlite ormlite

我的课程Coach包含ScheduleEntity个对象的集合;

public class Coach{

    @ForeignCollectionField(columnName = FIELD_SCHEDULE_ENTITY)
    private Collection<ScheduleEntity> scheduleEntities;
}

[...]

public class ScheduleEntity {

    @DatabaseField(columnName = FIELD_COACH, foreign = true)
    private Coach coach;
}

我想首先为教练检索ScheduleEntity

当我使用QueryBuilder执行此操作时:

public ScheduleEntity getFirstScheduleForCoach(Coach coach) throws SQLException {
    QueryBuilder<ScheduleEntity, Long> queryBuilder = scheduleEntityDao.queryBuilder();
    queryBuilder
            .where()
            .eq(ScheduleEntity.FIELD_COACH, coach);
   return scheduleEntityDao.queryForFirst(queryBuilder.prepare());

logcat中出现了一些奇怪的信息:

Close cursor android.database.sqlite.SQLiteCursor@42321aa0 on null twice or more

然而,当我以另一种方式这样做时:

return scheduleEntityDao.queryForEq(ScheduleEntity.FIELD_COACH, coach).iterator().next();

一切都很好(不要关心可能的无效指针)。

这些信息是什么意思?我做错了什么还是正常行为?

1 个答案:

答案 0 :(得分:4)

我继续下载了ORMLite的代码。

在第89行的AndroidCompiledStatement类中,我更改了close方法,如下所示:

public void close() throws IOException {
    if (cursor != null) {
        try {
            cursor.close();
        } catch (android.database.SQLException e) {
            throw new IOException("Problems closing Android cursor", e);
        }
    }
    cancellationHook = null;
}

public void close() throws IOException {
    if (cursor != null && !cursor.isClosed()) {
        try {
            cursor.close();
        } catch (android.database.SQLException e) {
            throw new IOException("Problems closing Android cursor", e);
        }
    }
    cancellationHook = null;
}

简单地检查一下,如果光标已经关闭了,那么对我来说几乎已经解决了这个问题,尽管你必须将ormlite-android git克隆到你的项目中。