【发布时间】:2019-02-15 07:41:40
【问题描述】:
我的 android 数据库和游标有问题。有时(很少)会发生,我从客户那里得到崩溃报告。很难找出它崩溃的原因,所以它确实是一些小错误。
这里是例外:
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
然后我有这样的方法(它在 c.moveToNext() 行中因该错误而崩溃)。它几乎从不崩溃,但有时会崩溃,但我无法重现它。
public List<AlarmModel> getAlarms() {
SQLiteDatabase db = this.getReadableDatabase();
String select = "SELECT * FROM " + Alarm.TABLE_NAME +" WHERE "+ Alarm.COLUMN_NAME_ALARM_FLAVOR_ID+" = -1 ";
Cursor c = db.rawQuery(select, null);
List<AlarmModel> alarmList = new ArrayList<AlarmModel>();
while (c.moveToNext()) {
alarmList.add(populateModel(c));
}
if (!alarmList.isEmpty()) {
if (db != null) {
db.close();
}
if (c != null) {
c.close();
}
return alarmList;
}
return null;
}
【问题讨论】:
-
首先,您应该在
if语句中使用c.moveToFist()并在您添加while循环代码的if正文中检查光标是否位于第一个位置。 -
谢谢!我正在尝试你的建议
-
那么为什么在while循环代码之前的if语句中使用c.moveToFist()?
-
这是为了检查游标是否有值并且应该从从db检索到的数据的第一个位置开始..
-
所以可以使用 (c != null) 代替 c.movetoFist() 吗?
标签: android