小编Abd*_*ved的帖子

androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap 的 NullPointerException

NullPointerException在 LazyColumn 中动态添加新项目时出现。根本原因是这个块androidx.compose.runtime.snapshots

private val readObserver: (Any) -> Unit = { state ->
        if (!isPaused) {
            synchronized(observedScopeMaps) {
                currentMap!!.recordRead(state)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

还附上崩溃日志

15:14:27.247 AndroidRuntime        D  Shutting down VM
15:14:27.250 AndroidRuntime        E  FATAL EXCEPTION: main
                                      Process: com.sdsol.starfish.debug, PID: 3256
                                      java.lang.NullPointerException
                                        at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.recordRead(SnapshotStateObserver.kt:404)
                                        at androidx.compose.runtime.snapshots.SnapshotStateObserver$readObserver$1.invoke(SnapshotStateObserver.kt:164)
                                        at androidx.compose.runtime.snapshots.SnapshotStateObserver$readObserver$1.invoke(SnapshotStateObserver.kt:161)
                                        at androidx.compose.runtime.snapshots.SnapshotKt.readable(Snapshot.kt:1904)
                                        at androidx.compose.runtime.SnapshotMutableStateImpl.getValue(SnapshotState.kt:135)
                                        at androidx.compose.ui.graphics.vector.VectorPainter.getSize-NH-jbRc$ui_release(VectorPainter.kt:425)
                                        at androidx.compose.ui.graphics.vector.VectorPainter.getIntrinsicSize-NH-jbRc(VectorPainter.kt:253)
                                        at androidx.compose.ui.draw.PainterNode.draw(PainterModifier.kt:304)
                                        at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105)
                                        at androidx.compose.ui.node.LayoutNodeDrawScope.draw-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:86)
                                        at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:365)
                                        at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:354)
                                        at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:182)
                                        at androidx.compose.ui.node.LayoutNodeDrawScope.drawContent(LayoutNodeDrawScope.kt:66)
                                        at androidx.compose.foundation.NoIndication$NoIndicationInstance.drawIndication(Indication.kt:136)
                                        at androidx.compose.foundation.IndicationModifier.draw(Indication.kt:183)
                                        at androidx.compose.ui.node.BackwardsCompatNode.draw(BackwardsCompatNode.kt:349)
                                        at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105)
                                        at androidx.compose.ui.node.LayoutNodeDrawScope.draw-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:86) …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-jetpack-compose

5
推荐指数
0
解决办法
709
查看次数

java.lang.IllegalStateException:对未放置的项目调用了replace()

问题出现在多个屏幕上。令我惊讶的是,相同的代码适用于以前版本的应用程序。我刚刚更新了撰写库,然后所有这些都在多个位置一次又一次地发生。

我正在使用 LazyColumn 来填充列表项。每当我尝试滚动它时,应用程序就会崩溃。下面是我正在使用的抽象代码。将 compose Bom 版本更新到2023-08-00后出现此问题。相同的代码适用于其他屏幕。这里唯一的区别是惰性列项几乎是屏幕高度的一半(1:1 宽高比)。所以每个屏幕大多有两个项目。

val users = viewModel.usersList

val lazyColumnListState = rememberLazyListState()

val shouldStartPaginate = remember {
    derivedStateOf {
        viewModel.canPaginate && (lazyColumnListState.layoutInfo.visibleItemsInfo.lastOrNull()?.index
            ?: -9) >= (lazyColumnListState.layoutInfo.totalItemsCount - 9)
    }
}

LaunchedEffect(key1 = shouldStartPaginate.value) {
    if (shouldStartPaginate.value && viewModel.listState == ListState.IDLE)
        viewModel.getMoreUsers()
}
//Inside Scafold
LazyColumn(
state = lazyColumnListState,
verticalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.padding(top = 16.dp)
) {
    items(
        items = users,
        key = { it.userID ?: (1000..5000).random() }) { i ->
        ComposeExploreItem(i)
    } …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-studio android-jetpack-compose

4
推荐指数
1
解决办法
1231
查看次数