androidandroid-jetpack-composeindexoutofboundsexceptionandroid-bottomsheetdialog

Compose BottomSheetScaffold IndexOutOfBoundsException application crash


I searched for a solution related to this issue but found nothing. I solved the problem so I'm posting the problem and the solution here with goal of helping someone.

Problem description

After updating Jetpack Compose from version 1.2.0-beta02 to 1.2.0-rc02, something on my screen caused an application to crash.

The code on this particular screen was not changed between the versions, so the crash had to be caused by one of the Compose components that I'm using. I managed to find the source of the crash by trial and error method and found out that the problem is BottomSheetScaffold component (with empty, kind of, content).

Code that causes a crash:

BottomSheetScaffold(
        sheetContent = { Text(text = "Some sheet content") }
    ) {
        AlertDialog(
            onDismissRequest = {
                // no-op
            },
            buttons = {
                Text(text = "here goes a button")
            }
        )
    }

Crash log:

2022-07-01 10:02:07.185 16257-16257/my.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: my.example.app, PID: 16257
    java.lang.IndexOutOfBoundsException: Index 0 is out of bounds. The list has 0 elements.
        at androidx.compose.runtime.collection.MutableVectorKt.checkIndex(MutableVector.kt:1135)
        at androidx.compose.runtime.collection.MutableVectorKt.access$checkIndex(MutableVector.kt:1)
        at androidx.compose.runtime.collection.MutableVector$MutableVectorList.get(MutableVector.kt:940)
        at androidx.compose.material.BottomSheetScaffoldKt$BottomSheetScaffoldLayout$1$1.invoke-0kLqBqw(BottomSheetScaffold.kt:447)
        at androidx.compose.material.BottomSheetScaffoldKt$BottomSheetScaffoldLayout$1$1.invoke(BottomSheetScaffold.kt:430)
        at androidx.compose.ui.layout.LayoutNodeSubcompositionsState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:590)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:44)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1428)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1427)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2101)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:110)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:78)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:66)
        at androidx.compose.ui.node.LayoutNode.performMeasure-BRTryo0$ui_release(LayoutNode.kt:1427)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:75)
        at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1366)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1.measure-3p2s80s(Box.kt:115)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:44)
        at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:405)
        at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:53)
        at androidx.compose.foundation.layout.FillModifier.measure-3p2s80s(Size.kt:658)
        at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:53)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1428)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1427)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2101)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:110)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:78)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:66)
        at androidx.compose.ui.node.LayoutNode.performMeasure-BRTryo0$ui_release(LayoutNode.kt:1427)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:94)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:75)
        at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1366)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1.measure-3p2s80s(Box.kt:115)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:44)
        at androidx.compose.ui.graphics.BlockGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:342)
        at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:53)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1428)
        at androidx.compose.ui.node.LayoutNode$performMeasure$1.invoke(LayoutNode.kt:1427)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2101)

Solution

  • BottomSheetScaffold content contains only AlertDialog component that is causing the crash (I guess it is treated as an empty content and empty content will cause the crash).

    After wrapping AlertDialog component in Box component, the crash was gone. I suppose there must be at least some screen content in the content argument (AlertDialog is probably not treated as a screen content because it is elevated - I'm not sure).

    Code that fixes the crash:

    BottomSheetScaffold(
            sheetContent = { Text(text = "Some sheet content") }
        ) {
            Box {
                AlertDialog(
                    onDismissRequest = {
                        // NO-OP
                    },
                    buttons = {
                        Text(text = "here goes a button")
                    }
                )
            }
        }
    

    Reference to the Google issue tracker where cause of the crash was found: https://issuetracker.google.com/issues/235588730