我正在使用 SQLite 数据库,我经常会遇到无法找到来源的运行时错误。查询后,我使用
moveToFirst
指向检索到的第一条记录,这有时会触发 android.database.CursorWindowAllocationException
异常。添加到此异常的是以下句子:“2048kb 的光标窗口分配失败。#open Cursors=736 (#cursors opendby this proc=736)”。
在Android文档中,我还没有找到任何与此异常相关的内容。有谁知道它的原因和避免它的方法吗?
这个错误几乎总是由于游标完成后没有关闭。每次打开游标时,都需要内存来映射游标所代表的数据,并且在关闭游标之前无法释放内存。可用于此目的的内存量是有限的,因此如果游标未关闭并且应用程序继续打开新游标,则可能会在某个时候发生此错误。
我建议您检查您的代码以确保所有创建的游标都在某个时候关闭。还要注意在循环中打开游标的任何代码 - 您的错误消息显示“open Cursors=736”,这表明在某种循环中有很多游标活动。
我也有同样的问题,但我正在关闭所有游标
Non-fatal Exception: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=1 (# cursors opened by this proc=1)
at android.database.CursorWindow.<init>(CursorWindow.java:108)
at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:198)
at android.database.sqlite.SQLiteCursor.clearOrCreateWindow(SQLiteCursor.java:309)
at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:147)
at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:141)
at com.xyz.abc.MyDataSource.kt:198
and my data source MyDataSource.kt is
fun getQuots(id: Long): ArrayList<Quot>{
val quots = ArrayList<Quot>()
var quot: Quot?
val cursor = getDb().query(TABLE_Qt, null,Qt_SURVEY_ID + " = " + id, null, null, null, null)
if (cursor.count > 0) {
cursor.moveToFirst()
while (!cursor.isAfterLast) {
quot = cursorToQuotModel(cursor)
if (null != quot) {
val app = mContext as MyApp
quot.items = getDetail(quot.detail_Id) //calling to get some details
quots.add(quot)
}
cursor.moveToNext()
}
}
cursor.close()
return quots
}
fun getDetail(id: Long): ArrayList<Item> {
val items = ArrayList<Item>()
val cursor = getDb().query(TABLE_Item, null,Item_QUOT_ID + "= ? ",
arrayOf(id.toString()), null, null, null)
cursor.moveToFirst()
while (!cursor.isAfterLast) {
val item = cursorToItem(cursor)
if (null != item) {
items.add(item)
}
cursor.moveToNext()
}
cursor.close()
return items
}