"pending composition has not been applied" 异常是什么意思,如何避免?
What does "pending composition has not been applied" exception mean and how can it be avoided?
我有一个正在运行的应用程序,但有时,由于我不知道的原因,应用程序崩溃并显示以下消息:
java.lang.IllegalStateException: pending composition has not been applied
我在任何地方都找不到关于这个异常发生的信息,我也不明白如何避免它。
编辑 #1:经过一些研究和测试,我发现当我在 BottomNavigation 中快速切换选项卡时会抛出此异常(选项卡包含 LazyColumn)
由于我没有足够的声誉来写评论我会写在这里。也许对你有帮助
我今天遇到了同样的错误。如果我开始调试并且我将大量日志记录行放入我的代码中以了解究竟是什么导致了这个问题。我弄清楚是什么导致了这个错误。我的代码中确实存在抛出异常(数组索引越界)的问题,但似乎某个地方的重组捕获了这个实际错误并抛出更多一般错误。
简化后的代码如下所示
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.invoke(Scaffold.kt:316)
at androidx.compose.material.ScaffoldKt$ScaffoldLayout.invoke(Scaffold.kt:243)
at androidx.compose.ui.layout.MeasureScope$layout.placeChildren(MeasureScope.kt:68)
at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$measure.placeChildren(SubcomposeLayout.kt:527)
at androidx.compose.ui.node.LayoutNode$layoutChildren.invoke(LayoutNode.kt:980)
at androidx.compose.ui.node.LayoutNode$layoutChildren.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.invoke(OuterMeasurablePlaceable.kt:146)
at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt.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)
所以至少对我来说,这个错误是在重组期间发生实际错误时抛出的。当然,如果它能向我显示实际错误而不是这个一般错误,那就太好了。
供日后参考,我曾经将 remember
错误地键入 rememberSavable
时遇到过这个问题。
我有一个正在运行的应用程序,但有时,由于我不知道的原因,应用程序崩溃并显示以下消息:
java.lang.IllegalStateException: pending composition has not been applied
我在任何地方都找不到关于这个异常发生的信息,我也不明白如何避免它。
编辑 #1:经过一些研究和测试,我发现当我在 BottomNavigation 中快速切换选项卡时会抛出此异常(选项卡包含 LazyColumn)
由于我没有足够的声誉来写评论我会写在这里。也许对你有帮助
我今天遇到了同样的错误。如果我开始调试并且我将大量日志记录行放入我的代码中以了解究竟是什么导致了这个问题。我弄清楚是什么导致了这个错误。我的代码中确实存在抛出异常(数组索引越界)的问题,但似乎某个地方的重组捕获了这个实际错误并抛出更多一般错误。
简化后的代码如下所示
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.invoke(Scaffold.kt:316)
at androidx.compose.material.ScaffoldKt$ScaffoldLayout.invoke(Scaffold.kt:243)
at androidx.compose.ui.layout.MeasureScope$layout.placeChildren(MeasureScope.kt:68)
at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$measure.placeChildren(SubcomposeLayout.kt:527)
at androidx.compose.ui.node.LayoutNode$layoutChildren.invoke(LayoutNode.kt:980)
at androidx.compose.ui.node.LayoutNode$layoutChildren.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.invoke(OuterMeasurablePlaceable.kt:146)
at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt.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)
所以至少对我来说,这个错误是在重组期间发生实际错误时抛出的。当然,如果它能向我显示实际错误而不是这个一般错误,那就太好了。
供日后参考,我曾经将 remember
错误地键入 rememberSavable
时遇到过这个问题。