cursor.moveToNext()引发CursorWindowAllocationException

时间:2019-02-25 12:00:57

标签: android sqlite kotlin android-sqlite

由于此异常,我已经崩溃了几次,但是无法在自己的任何设备/仿真器上复制它。

来自Google Play控制台的stacktrace

  android.database.CursorWindowAllocationException: 
  at android.database.CursorWindow.<init> (CursorWindow.java:108)
  at android.database.AbstractWindowedCursor.clearOrCreateWindow (AbstractWindowedCursor.java:198)
  at android.database.sqlite.SQLiteCursor.fillWindow (SQLiteCursor.java:138)
  at android.database.sqlite.SQLiteCursor.getCount (SQLiteCursor.java:132)
  at android.database.AbstractCursor.moveToPosition (AbstractCursor.java:220)
  at android.database.AbstractCursor.moveToNext (AbstractCursor.java:269)
  at rpuls.yatzysheets.DBController.getGameTime (DBController.kt:264)
  at rpuls.yatzysheets.PreviousGames$onCreateView$1.invoke (PreviousGames.kt:78)
  at rpuls.yatzysheets.PreviousGames$onCreateView$1.invoke (PreviousGames.kt:23)
  at org.jetbrains.anko.support.v4.SupportKt.UI (Support.kt:62)
  at rpuls.yatzysheets.PreviousGames.onCreateView (PreviousGames.kt:33)
  at android.support.v4.app.Fragment.performCreateView (Fragment.java:2337)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1419)
  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1740)
  at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1809)
  at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:799)
  at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2580)
  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2367)
  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute (FragmentManager.java:2322)
  at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2229)
  at android.support.v4.app.FragmentManagerImpl$1.run (FragmentManager.java:700)
  at android.os.Handler.handleCallback (Handler.java:790)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6651)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:810)

正在抛出它的函数

fun getGameTime(gameId: Long): String{
    val db = this.readableDatabase
    val cursor = db.rawQuery("SELECT ${gameTable.timestamp} FROM ${gameTable.table} WHERE ${gameTable.id} = ${gameId}", null)
    var result = ""
    if(cursor.moveToNext()){
        result = cursor.getString(cursor.getColumnIndex(gameTable.timestamp))
    }
    db.close()
    return result
}

当我搜索 CursorWindowAllocationException 时,几乎所有结果都与不关闭游标以及导致moveToNext()的内存不足导致抛出异常有关。

但是,在堆栈跟踪中,我没有什么用尽的内存,而且我相信我的游标将关闭,因为它所基于的可读数据库已关闭?

发生错误的设备

基本上是配备2GB内存的现代手机。在过去的60天内,它发生了一次,但是几个月前,我已经看到了。

该应用每天大约可在40台设备上运行,因此2400次运行中只有1次崩溃-数量不多,但我想知道为什么会这样。

2 个答案:

答案 0 :(得分:1)

我想说你的工作cursor有点奇怪。而且我不确定cursor在数据库关闭时是否将关闭。我仍然建议您这样做:

if (cursor.moveToFirst()) {
    result = cursor.getString(cursor.getColumnIndex(gameTable.timestamp))
}
cursor.close()
db.close()

答案 1 :(得分:-1)

尝试使用cursor.moveToFirst()。

fun getGameTime(gameId: Long): String{
        val db = this.readableDatabase
        val cursor = db.rawQuery("SELECT ${gameTable.timestamp} FROM ${gameTable.table} WHERE ${gameTable.id} = ${gameId}", null)
        var result = ""
        if(cursor.moveToFirst()){
            result = cursor.getString(cursor.getColumnIndex(gameTable.timestamp))
        }
        db.close()
        return result
    }