android “尚未适用未决组成”例外是什么意思?如何避免?

8ehkhllq  于 2022-11-20  发布在  Android
关注(0)|答案(5)|浏览(137)

我有一个正在运行的应用程序,但有时,由于我不知道的原因,应用程序崩溃并显示以下消息:
java.lang.IllegalStateException:尚未应用待定组合
我在任何地方都找不到有关此异常发生的信息,我也不明白如何避免它。
编辑#1:经过一些研究和测试,我发现当我在BottomNavigation中的选项卡之间快速切换时(选项卡包含LazyColumn),会抛出这个异常

webghufk

webghufk1#

为了将来的参考,我曾经遇到过这个问题,当我键入remember错误地为rememberSavable

lzfw57am

lzfw57am2#

因为我没有足够的声誉写评论,我会写在这里。也许它会帮助你
今天我遇到了同样的错误。如果我开始调试,我在代码中放入了很多日志行,以了解到底是什么导致了这个问题。我找到了导致这个错误的原因。在我的代码中确实存在抛出异常的问题(数组索引超出界限),但似乎在某个地方重新组合捕获了这个实际错误,并抛出了更一般的错误。
简化后的代码如下所示

val items = myViewModel.items.collectAsState().value
val focusRequesters = myViewModel.focusRequesters.collectAsState().value
LazyColumn() {
    items(items = items) { item ->
        // When I added new item then I got error in this line, 
        // because I forgot to add new focusRequester when I was adding new item. 
        // so it was actually array index out of bounds error
        val focusRequester = focusRequesters[item.position]
        ...
        // Code to show item
    }
    item {
         AddItemButton(myViewModel)
    }
}

它在日志中显示的实际错误是此一般错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.courtesy.test, PID: 27394
    java.lang.IllegalStateException: pending composition has not been applied
        at androidx.compose.runtime.CompositionImpl.drainPendingModificationsForCompositionLocked(Composition.kt:530)
        at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:567)
        at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:811)
        at androidx.compose.runtime.ComposerImpl$CompositionContextImpl.composeInitial$runtime_release(Composer.kt:3583)
        at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:503)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcomposeInto(SubcomposeLayout.kt:408)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:383)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:374)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:363)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$Scope.subcompose(SubcomposeLayout.kt:648)
        at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1.invoke(Scaffold.kt:316)
        at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1.invoke(Scaffold.kt:243)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$1$measure$1.placeChildren(SubcomposeLayout.kt:527)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:980)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:965)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2063)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:112)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:78)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:52)
        at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:965)
        at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:950)
        at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:78)
        at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:31)
        at androidx.compose.ui.layout.Placeable$PlacementScope.place-70tqf50(Placeable.kt:370)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:158)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.access$placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:28)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:146)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:145)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2063)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:112)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:78)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:59)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeAt-f8xVGno(OuterMeasurablePlaceable.kt:145)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.replace(OuterMeasurablePlaceable.kt:170)
        at androidx.compose.ui.node.LayoutNode.replace$ui_release(LayoutNode.kt:838)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:262)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:36)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:201)
E/AndroidRuntime:     at androidx.compose.ui.platform.AndroidComposeView.onMeasure(AndroidComposeView.android.kt:773)
        at android.view.View.measure(View.java:25466)
        at androidx.compose.ui.platform.AbstractComposeView.internalOnMeasure$ui_release(ComposeView.android.kt:298)
        at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:285)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6957)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:747)
        at android.view.View.measure(View.java:25466)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3397)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:2228)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2486)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1952)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8171)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:731)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

所以至少对我来说,这个错误是在重组过程中发生实际错误时引发的。当然,如果它能向我显示实际错误而不是这个一般错误,那就太好了。

dfuffjeb

dfuffjeb3#

我遇到了同样的问题,当我有一个setContent来告诉片段我将从那一点开始进行组合,并在可组合树中向下添加一个Composable来处理map-〉GoogleMap
问题是我忘了添加Map所需的API_KEY,因此在重新组合过程中捕获了抛出的异常,这导致了显示该错误。
所以我想如果你有这个问题,检查一下重组是否掩盖了真实的的错误。怎么做?
尝试将引发该一般性错误的可组合代码在层次结构中尽可能上移,以便看到真实的的错误。

mwg9r5ms

mwg9r5ms4#

我认为这是发生在我身上,因为我有我的TabRows指示器TabPosition设置为TabPosition[lazyListState.firstVisibleItemIndex],不能找出解决方案是什么。
但我本质上希望指标随着懒惰列表滚动可见项而更新

wlwcrazw

wlwcrazw5#

所以对我来说,问题是我用了两倍的setContent来代替ComposeView
范例
我用这个

composeView.setContet {
 composeView.setContent { ... return@setContent }
}

我只需要用一个

composeView.setContent { ... return@setContent }

相关问题