应用程序在Jetpack Compose的Lazycolumn中由于IndexOutOfBoundsException崩溃。

12
我在mutableStateListOf中删除项目时遇到了IndexOutOfBoundsException错误,其中MyType是一种自定义类型。myList中大约有250个项目。这些项目在循环中被删除。一次最多可以看到6个元素。当项目数达到小于或等于6(滚动位置在顶部)时,就会引发此异常并导致应用程序崩溃。
此外,只有当ListView是活动屏幕时,应用程序才会崩溃。否则,它不会崩溃/不会引发此indexoutofboundexception。我已经调试了应用程序逻辑,并确保有检查来忽略任何索引大于列表大小的更新/添加/删除请求。我怀疑异常只在重新组合时引发。我还观察到的另一种行为是当列表滚动到底部并且项目被删除时,此异常已经在循环的开始处引发。请查看堆栈跟踪。

致命异常: 主要进程: com.XXX.YYY.ZZZ, PID: 16225 java.lang.IndexOutOfBoundsException: 索引 6,大小 6 在 androidx.compose.foundation.lazy.layout.MutableIntervalList.checkIndexBounds(IntervalList.kt:177) 中 at androidx.compose.foundation.lazy.layout.MutableIntervalList.get(IntervalList.kt:160) at androidx.compose.foundation.lazy.layout.DefaultLazyLayoutItemsProvider.getKey(LazyLayoutItemProvider.kt:232) at androidx.compose.foundation.lazy.LazyListItemProviderImpl.getKey(Unknown Source:2) at androidx.compose.foundation.lazy.layout.DefaultDelegatingLazyLayoutItemProvider.getKey(LazyLayoutItemProvider.kt:201) at androidx.compose.foundation.lazy.LazyListItemProviderKt$rememberLazyListItemProvider$1$1.getKey(Unknown Source:2) at androidx.compose.foundation.lazy.LazyMeasuredItemProvider.getAndMeasure-ZjPyQlc(LazyMeasuredItemProvider.kt:46) at androidx.compose.foundation.lazy.LazyListMeasureKt.measureLazyList-Hh3qtAg(LazyListMeasure.kt:164) at androidx.compose.foundation.lazy.LazyListKt$rememberLazyListMeasurePolicy$1$1.invoke-0kLqBqw(LazyList.kt:299) at androidx.compose.foundation.lazy.LazyListKt$rememberLazyListMeasurePolicy$1$1.invoke(LazyList.kt:190) at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$1$2$1.invoke-0kLqBqw(LazyLayout.kt:71) at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$1$2$1.invoke(LazyLayout.kt:69) at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:598) at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:103) at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:635) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:155) at androidx.compose.foundation.layout.SizeModifier.measure-3p2s80s(Size.kt:781) at androidx.compose.ui.node.BackwardsCompatNode.measure-3p2s80s(BackwardsCompatNode.kt:323) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:155) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1090) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasure$2.invoke(LayoutNodeLayoutDelegate.kt:1086) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2200) at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:234) at androidx.compose.runtime.snapshots.SnapshotStateObserver$observeReads$1$1.invoke(SnapshotStateObserver.kt:230) at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341) at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:230) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:120) at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:107) at androidx.compose.ui.node.LayoutNodeLayoutDelegate.performMeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:1086) at androidx.compose.ui.node.LayoutNodeLayoutDelegate.access$performMeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:36) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.remeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:342) at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release(LayoutNode.kt:1188) at androidx.compose.ui.node.LayoutNode.remeasure-_Sx5XlM$ui_release$default(LayoutNode.kt:1179) at androidx.compose.ui.node.MeasureAndLayoutDelegate.doRemeasure-sdFAvZA(MeasureAndLayoutDelegate.kt:309) at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:434) 2023-07-03 15:30:55.228 16225-16225 AndroidRuntime com.com.XXX.YYY.ZZZ E

我正在使用以下的Jetpack Compose版本:
```kotlin ext.composeCompilerVersion = 1.4.3
ext { compose_version = composeCompilerVersion // COMPOSE编译器版本 } ```
以下是用于显示ListView的示例组合函数。
@Composable
fun ListViewInternalTest(
    elements2: List<MyType>
) {
    val listState = rememberLazyListState()

    LazyColumn(
        modifier = Modifier
            .height(pixelToDp(pixelSize = 792))
            .simpleVerticalScrollbar(state = listState),
        state = listState
    ) {

        items(
            elements2.size
        ) { index ->

            Column() {
                Row(
                    modifier = Modifier
                        .padding(start = pixelToDp(pixelSize = 35))
                        .height(pixelToDp(pixelSize = 130)),
                    verticalAlignment = Alignment.CenterVertically
                ) {

                    if(index < elements2.size) {
                        val element = elements2.getOrNull(index)

                        if (element != null) {

                            Text(
                                 modifier = Modifier
                                      .align(Alignment.Center)
                                      .width(pixelToDp(pixelSize = 202))
                                      .alpha(1f),
                                 text = "CH " + element.someMember.toString()
                                        )


                        }
                    }
                }
            }
        }
    }
}

val myList = mutableStateListOf<MyType>()

    fun insertItemToMyListAtIndex(index: Int, argMyType: MyType){

        if(index >= 0 && index <= myList.size) {
            myList.add(index, argMyType)
        }
    }

    fun updateItemInMyListAtIndex(index: Int, argMyType: MyType) {

        if(index>= 0 && index< myList .size) {
            myList[index] = argMyType
        }
    }

    fun removeItemFromMyList(index: Int) {

        if(index >= 0 && index < myList.size) {
            myList.removeAt(index)
        }
    }

    fun clearAllitemsInMyList(){
        myList.clear()
    }

我尝试重现这个行为。首先,我发现如果ListView不是活动屏幕(因为ListView没有重新组合),就不会引发此异常。另外,在查看堆栈跟踪后,我的假设是这个问题与内部的Jetpack Compose库或我实现的可组合函数(即Lazycolumn)有关。

你找到了这个问题的解决办法吗?如果我从列表中移除一个项目,我也会遇到同样的问题。 - undefined
我遇到了同样的错误。有什么帮助吗? - undefined
3个回答

1

0
这是我的问题 implementation("androidx.compose.material3:material3:1.2.0-alpha11")
我将其改为 implementation("androidx.compose.material3:material3")
问题得到解决。我不知道这些东西是如何关联的。但是我花了一些时间才弄清楚。希望这对其他人有所帮助。

0
在你的代码中尝试使用下面的代码并运行。
@Composable
fun ListViewInternalTest(
elements2: List<MyType>
) {
    val listState = rememberLazyListState()

LazyColumn(
    modifier = Modifier
        .height(pixelToDp(pixelSize = 792))
        .simpleVerticalScrollbar(state = listState),
    state = listState
) {

    items(
        elements2
    ) { index, element ->

        Column() {
            Row(
                modifier = Modifier
                    .padding(start = pixelToDp(pixelSize = 35))
                    .height(pixelToDp(pixelSize = 130)),
                verticalAlignment = Alignment.CenterVertically
            ) {
                                          
                      Text(
                             modifier = Modifier
                                  .align(Alignment.Center)
                                  .width(pixelToDp(pixelSize = 202))
                                  .alpha(1f),
                             text = "CH " + element.someMember.toString()
                                    )


                    }
                }
            }
        }
    }
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接