崩溃-尝试重新打开已关闭的对象:sqlitedatabase

hpxqektj  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(228)

因此,我有以下代码,由于某些原因,在运行时有时会导致崩溃:

fun getTransactionTotalForCategoryDayMonthYear(
    categoryId: Int,
    day: Int,
    month: Int,
    year: Int
): Float {
    var amount: String
    var runningTotal = 0.00F
    val dbTrans = this.readableDatabase
    val cursor = dbTrans.rawQuery(
        "SELECT * FROM $TABLE_TRANSACTIONS WHERE $KEY_CATEGORY = $categoryId AND $KEY_DAY = $day AND $KEY_MONTH = $month AND $KEY_YEAR = $year",
        null
    )

    if (cursor.moveToFirst()) {
        do {
            amount = cursor.getString(cursor.getColumnIndex(KEY_AMOUNT))
            runningTotal += amount.toFloat()
        } while (cursor.moveToNext())
    }

    cursor.close()
    dbTrans.close()

    return runningTotal

}

问题是当对dbtrans调用rawquery时,dbtrans显然是关闭的,但是当它在它上面的行中启动时,我看不出这种情况。有人知道为什么会这样吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题