Jetpack compose LazyVerticalGrid items throws java.lang.IllegalStateException
Asked Answered
F

4

7

I'm facing weird exception while placing all images from internal storage in LazyVerticalGrid. It was working fine without any problem till now, but recently this problem appears without any noticeable hint.

Here's the my ui and utility code

fun getAllImagesFromStorage(context: Context): List<ImageMetaData> {
    val imageList = mutableListOf<ImageMetaData>()
    val collection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
    } else {
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    }
    val projection = arrayOf(
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.DISPLAY_NAME,
        MediaStore.Images.Media.SIZE
    )
    val sortOrder = MediaStore.MediaColumns.DATE_ADDED + " DESC"

    val cursor = context.contentResolver
        .query(collection, projection, null, null, sortOrder)

    cursor?.use { cursor ->
        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
        val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)

        while (cursor.moveToNext()) {
            val id = cursor.getLong(idColumn)
            val name = cursor.getString(nameColumn)

            val contentUri: Uri = ContentUris.withAppendedId(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                id
            )
            imageList += ImageMetaData(
                id = id,
                name = name,
                uri = contentUri
            )
        }
    }
    return imageList
}

@Composable
fun GalleryUi() {
    val imageList by remember { mutableStateOf<List<ImageMetaData>?>(getAllImagesFromStorage(context)) }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(modifier = Modifier.weight(1f)) {
            Image(
                modifier = Modifier.fillMaxSize(),
                painter = rememberImagePainter(data = uri),
                contentDescription = null,
                contentScale = contentScale
            )
        }

        ImageGridView(
            modifier = Modifier.weight(1f),
            context = context,
            images = imageList,
            onImageSelected = { imageMetaData ->
                onImageSelected(imageMetaData.uri)
            }
        )
    }
}

@ExperimentalFoundationApi
@Composable
private fun ImageGridView(
    modifier: Modifier = Modifier,
    context: Context,
    images: List<ImageMetaData>,
    onImageSelected: (ImageMetaData) -> Unit
) {
    LazyVerticalGrid(
        modifier = modifier,
        cells = GridCells.Fixed(4)
    ) {
        items(images) { image ->
            // Using remember{ } value for bitmap can cause lagging while scrolling images
            var bitmap: Bitmap? = null
            try {
                bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    context.contentResolver.loadThumbnail(image.uri, Size(640, 480), null)
                } else {
                    MediaStore.Images.Thumbnails.getThumbnail(
                        context.contentResolver,
                        image.id,
                        MediaStore.Images.Thumbnails.MINI_KIND,
                        null
                    )
                }
            } catch (e: IOException) {
            }

            bitmap?.let {
                Image(
                    modifier = Modifier
                        .fillMaxWidth()
                        .aspectRatio(1f)
                        .border(1.dp, Color.White)
                        .clickable { onImageSelected(image) },
                    bitmap = it.asImageBitmap(),
                    contentDescription = null,
                    contentScale = ContentScale.Crop
                )
            }
        }
    }
}

Here's the exception that i'm getting

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rajesh.client, PID: 14047
    java.lang.IllegalStateException: Check failed.
        at androidx.compose.foundation.lazy.LazyGridKt$ItemRow$2.measure-3p2s80s(LazyGrid.kt:451)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:55)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:99)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:76)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:75)
        at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1308)
        at androidx.compose.foundation.lazy.layout.LazyLayoutPlaceablesProvider.getAndMeasure-0kLqBqw(LazyMeasurePolicy.kt:61)
        at androidx.compose.foundation.lazy.list.LazyMeasuredItemProvider.getAndMeasure-oA9-DU0(LazyMeasuredItemProvider.kt:45)
        at androidx.compose.foundation.lazy.list.LazyListMeasureKt.measureLazyList-wroFCeY(LazyListMeasure.kt:145)
        at androidx.compose.foundation.lazy.list.LazyListKt$rememberLazyListMeasurePolicy$1$1.measure-3p2s80s(LazyList.kt:259)
        at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$2.invoke-0kLqBqw(LazyLayout.kt:55)
        at androidx.compose.foundation.lazy.layout.LazyLayoutKt$LazyLayout$2.invoke(LazyLayout.kt:44)
        at androidx.compose.ui.layout.SubcomposeLayoutState$createMeasurePolicy$1.measure-3p2s80s(SubcomposeLayout.kt:355)
        at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:55)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:306)
        at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:39)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.measure-BRTryo0(DelegatingLayoutNodeWrapper.kt:131)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:99)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:76)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:75)
        at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1308)
        at androidx.compose.foundation.layout.RowColumnImplKt$rowColumnMeasurePolicy$1.measure-3p2s80s(RowColumnImpl.kt:147)
E/AndroidRuntime:     at androidx.compose.ui.node.InnerPlaceable.measure-BRTryo0(InnerPlaceable.kt:55)
        at androidx.compose.foundation.layout.FillModifier.measure-3p2s80s(Size.kt:658)
        at androidx.compose.ui.node.ModifiedLayoutNode.measure-BRTryo0(ModifiedLayoutNode.kt:39)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:99)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$remeasure$2.invoke(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:76)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.remeasure-BRTryo0(OuterMeasurablePlaceable.kt:98)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.measure-BRTryo0(OuterMeasurablePlaceable.kt:75)
        at androidx.compose.ui.node.LayoutNode.measure-BRTryo0(LayoutNode.kt:1308)
        at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1.invoke(Scaffold.kt:317)
        at androidx.compose.material.ScaffoldKt$ScaffoldLayout$1$1$1.invoke(Scaffold.kt:241)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.layout.SubcomposeLayoutState$createMeasurePolicy$1$measure$1.placeChildren(SubcomposeLayout.kt:367)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:993)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:978)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:62)
        at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:978)
        at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:964)
        at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:129)
        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:170)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.access$placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:28)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:158)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:157)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:69)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeAt-f8xVGno(OuterMeasurablePlaceable.kt:157)
        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.layout.Placeable$PlacementScope.place-70tqf50$default(Placeable.kt:203)
        at androidx.compose.foundation.layout.BoxKt.placeInBox(Box.kt:186)
        at androidx.compose.foundation.layout.BoxKt.access$placeInBox(Box.kt:1)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$2.invoke(Box.kt:126)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$2.invoke(Box.kt:125)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:993)
E/AndroidRuntime:     at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:978)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:62)
        at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:978)
        at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:964)
        at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:129)
        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.layout.Placeable$PlacementScope.place-70tqf50$default(Placeable.kt:203)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper$measure$1$1.placeChildren(DelegatingLayoutNodeWrapper.kt:138)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.placeAt-f8xVGno(DelegatingLayoutNodeWrapper.kt:126)
        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.layout.Placeable$PlacementScope.place-70tqf50$default(Placeable.kt:203)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper$measure$1$1.placeChildren(DelegatingLayoutNodeWrapper.kt:138)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.placeAt-f8xVGno(DelegatingLayoutNodeWrapper.kt:126)
        at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:31)
        at androidx.compose.ui.layout.Placeable$PlacementScope.placeWithLayer(Placeable.kt:393)
        at androidx.compose.ui.layout.Placeable$PlacementScope.placeWithLayer$default(Placeable.kt:266)
        at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier$measure$1.invoke(GraphicsLayerModifier.kt:308)
        at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier$measure$1.invoke(GraphicsLayerModifier.kt:307)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.placeAt-f8xVGno(DelegatingLayoutNodeWrapper.kt:126)
        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:170)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.access$placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:28)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:158)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:157)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:69)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeAt-f8xVGno(OuterMeasurablePlaceable.kt:157)
        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.layout.Placeable$PlacementScope.place-70tqf50$default(Placeable.kt:203)
        at androidx.compose.foundation.layout.BoxKt.placeInBox(Box.kt:186)
        at androidx.compose.foundation.layout.BoxKt.access$placeInBox(Box.kt:1)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$2.invoke(Box.kt:126)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$2.invoke(Box.kt:125)
E/AndroidRuntime:     at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:993)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:978)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:62)
        at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:978)
        at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:964)
        at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:129)
        at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:31)
        at androidx.compose.ui.layout.Placeable$PlacementScope.placeWithLayer(Placeable.kt:393)
        at androidx.compose.ui.layout.Placeable$PlacementScope.placeWithLayer$default(Placeable.kt:266)
        at androidx.compose.ui.graphics.BlockGraphicsLayerModifier$measure$1.invoke(GraphicsLayerModifier.kt:249)
        at androidx.compose.ui.graphics.BlockGraphicsLayerModifier$measure$1.invoke(GraphicsLayerModifier.kt:248)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.placeAt-f8xVGno(DelegatingLayoutNodeWrapper.kt:126)
        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:170)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.access$placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:28)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:158)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:157)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:69)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeAt-f8xVGno(OuterMeasurablePlaceable.kt:157)
        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.layout.Placeable$PlacementScope.place-70tqf50$default(Placeable.kt:203)
        at androidx.compose.foundation.layout.BoxKt.placeInBox(Box.kt:186)
        at androidx.compose.foundation.layout.BoxKt.access$placeInBox(Box.kt:1)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$5.invoke(Box.kt:167)
        at androidx.compose.foundation.layout.BoxKt$boxMeasurePolicy$1$measure$5.invoke(Box.kt:163)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:993)
        at androidx.compose.ui.node.LayoutNode$layoutChildren$1.invoke(LayoutNode.kt:978)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:126)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutSnapshotReads$ui_release(OwnerSnapshotObserver.kt:62)
        at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:978)
        at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:964)
        at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:129)
E/AndroidRuntime:     at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:31)
        at androidx.compose.ui.layout.Placeable$PlacementScope.place(Placeable.kt:367)
        at androidx.compose.ui.layout.Placeable$PlacementScope.place$default(Placeable.kt:191)
        at androidx.compose.foundation.layout.PaddingValuesModifier$measure$2.invoke(Padding.kt:422)
        at androidx.compose.foundation.layout.PaddingValuesModifier$measure$2.invoke(Padding.kt:421)
        at androidx.compose.ui.layout.MeasureScope$layout$1.placeChildren(MeasureScope.kt:68)
        at androidx.compose.ui.node.DelegatingLayoutNodeWrapper.placeAt-f8xVGno(DelegatingLayoutNodeWrapper.kt:126)
        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:170)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.access$placeOuterWrapper-f8xVGno(OuterMeasurablePlaceable.kt:28)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:158)
        at androidx.compose.ui.node.OuterMeasurablePlaceable$placeAt$1.invoke(OuterMeasurablePlaceable.kt:157)
        at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:1798)
        at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:121)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:88)
        at androidx.compose.ui.node.OwnerSnapshotObserver.observeLayoutModifierSnapshotReads$ui_release(OwnerSnapshotObserver.kt:69)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.placeAt-f8xVGno(OuterMeasurablePlaceable.kt:157)
        at androidx.compose.ui.node.OuterMeasurablePlaceable.replace(OuterMeasurablePlaceable.kt:182)
        at androidx.compose.ui.node.LayoutNode.replace$ui_release(LayoutNode.kt:855)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:234)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:38)
        at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:201)
        at androidx.compose.ui.platform.AndroidComposeView.measureAndLayout(AndroidComposeView.android.kt:662)
        at androidx.compose.ui.node.Owner$DefaultImpls.measureAndLayout$default(Owner.kt:182)
        at androidx.compose.ui.platform.AndroidComposeView.dispatchDraw(AndroidComposeView.android.kt:846)
        at android.view.View.draw(Unknown Source:58)
        at android.view.View.updateDisplayListIfDirty(Unknown Source:188)
        at android.view.ViewGroup.recreateChildDisplayList(Unknown Source:21)
        at android.view.ViewGroup.dispatchGetDisplayList(Unknown Source:21)
        at android.view.View.updateDisplayListIfDirty(Unknown Source:77)
        at android.view.ViewGroup.recreateChildDisplayList(Unknown Source:21)
        at android.view.ViewGroup.dispatchGetDisplayList(Unknown Source:21)
        at android.view.View.updateDisplayListIfDirty(Unknown Source:77)
        at android.view.ViewGroup.recreateChildDisplayList(Unknown Source:21)
        at android.view.ViewGroup.dispatchGetDisplayList(Unknown Source:21)
        at android.view.View.updateDisplayListIfDirty(Unknown Source:77)
        at android.view.ViewGroup.recreateChildDisplayList(Unknown Source:21)
        at android.view.ViewGroup.dispatchGetDisplayList(Unknown Source:21)
        at android.view.View.updateDisplayListIfDirty(Unknown Source:77)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(Unknown Source:27)
        at android.view.ThreadedRenderer.updateRootDisplayList(Unknown Source:7)
        at android.view.ThreadedRenderer.draw(Unknown Source:9)
        at android.view.ViewRootImpl.draw(Unknown Source:463)
        at android.view.ViewRootImpl.performDraw(Unknown Source:112)
        at android.view.ViewRootImpl.performTraversals(Unknown Source:2864)
        at android.view.ViewRootImpl.doTraversal(Unknown Source:31)
E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(Unknown Source:2)
        at android.view.Choreographer$CallbackRecord.run(Unknown Source:20)
        at android.view.Choreographer.doCallbacks(Unknown Source:79)
        at android.view.Choreographer.doFrame(Unknown Source:182)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Unknown Source:9)
        at android.os.Handler.handleCallback(Unknown Source:2)
        at android.os.Handler.dispatchMessage(Unknown Source:4)
        at android.os.Looper.loop(Unknown Source:242)
        at android.app.ActivityThread.main(Unknown Source:98)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
        at com.android.internal.os.ZygoteInit.main(Unknown Source:275)

UPDATE I've provided solution below

Farrow answered 5/1, 2022 at 9:20 Comment(1)
Sadly I face this issue from yesterday, too. I was thinking that it's related to latest update but when I rolled everything back I confused because same code without any touch is not working now.Labelle
L
0

In my case I was checking null value and returning if value was null. while LazyVerticalGrid using child composable item's to calculate width and I was returning before any measurement and it causes occur exception.

something like this

@Composable
fun SimpleGridChild(title: String?) {
    if (title == null) return
    Text(text = title)
}
Labelle answered 6/1, 2022 at 7:58 Comment(3)
Did it solved your issue? Can you share some code snippet?Farrow
yes, I added a simple exampleLabelle
The code you posted is solution or problem? I tried returning in case of null value, but still facing same issue. bitmap?.let { Image(bitmap = it.asImageBitmap()) } ?: return@itemsFarrow
F
0

I found that issue was with image itself. Actual image was deleted from storage, but (i guess) its entry was present in MediaStore. So Image(bitmap = it.asImageBitmap()) was not able decode it and thus not having any child in LazyVerticalGrid, which was throwing exception. I managed to resolve this by using below code

bitmap?.let {
    Image(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(1f)
            .border(1.dp, Color.White)
            .clickable { onImageSelected(image) },
        bitmap = it.asImageBitmap(),
        contentDescription = null,
        contentScale = ContentScale.Crop
    )
} ?: Box(
    modifier = Modifier
        .fillMaxWidth()
        .aspectRatio(1f)
        .border(1.dp, Color.White)
        .clickable {
            Toast
                .makeText(
                    context,
                    context.getString(R.string.media_format_not_supported),
                    Toast.LENGTH_SHORT
                )
                .show()
        }
) {
    Icon(
        modifier = Modifier
            .size(45.dp)
            .align(Alignment.Center),
        imageVector = Icons.Default.Warning,
        contentDescription = null,
    )
}

However, i was not able to completely ignore that child by using below code

bitmap?.let {
    Image(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(1f)
            .border(1.dp, Color.White)
            .clickable { onImageSelected(image) },
        bitmap = it.asImageBitmap(),
        contentDescription = null,
        contentScale = ContentScale.Crop
    )
} ?: return@items
Farrow answered 6/1, 2022 at 14:26 Comment(0)
R
0

For me, it was caused by having an item block inside the LazyVerticalGrid() block. Any extra composables must be added outside the items block, or as extra items block inside the LazyVerticalGrid() composable, as illustrated below:

// ... inside a @Composable
Column {
    LazyVerticalGrid(
        cells = GridCells.Fixed(2),
    ) {
        items(theItems) { item ->
            Row {
                Text(
                    text = item.name,
                )
            }
        }
        // Inside the block
        items(1) {
            Button(onClick = {
                scope.launch {
                    viewModel.startSim()
                }
            }) {
                Text(text = "Start Sim")
            }
        }
        items(1){
            Button(onClick = {
                scope.launch {
                    viewModel.stopSim()
                }
            }) {
                Text(text = "Stop Sim")
            }
        }
    }
    
    // ... or outside the block.
    Button(onClick = {
        scope.launch {
            viewModel.startSim()
        }
    }) {
        Text(text = "Start Sim")
    }

    Button(onClick = {
        scope.launch {
            viewModel.stopSim()
        }
    }) {
        Text(text = "Stop Sim")
    }
}
Rictus answered 6/10, 2022 at 22:13 Comment(0)
T
0

in my case, that exception was when inside "let" construction i was trying to call that state once more, but not THIS actual state.

state.detailedOrder?.let { order ->
                BasicAlertDialog(
                    modifier = Modifier.padding(20.dp).widthIn(min = 720.dp),
                    properties = DialogProperties(usePlatformDefaultWidth = false, dismissOnClickOutside = false),
                    onDismissRequest = {
                        vm.onEvent(
                            LastOrdersEvent.DialogDismissed
                        )
                    }
                ) {
                    Column(
                        Modifier.wrapContentSize()
                            .background(color = Color.White, shape = RoundedCornerShape(16.dp))
                            .padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        OutlinedCard(
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            LazyVerticalGrid(
                                columns = GridCells.Fixed(1),
                                horizontalArrangement = Arrangement.spacedBy(8.dp),
                            ) {
                                items(
                                    state.detailedOrder!!.orderUnits.sortedBy { it.id },
                                    span = { GridItemSpan(maxLineSpan) }
                                ) {
                                    state.listOfDetailedCompositionModel.forEach { detailedCompositionModel ->
                                        for (shipping in state.detailedShippingList) {
                                            for (sUnit in shipping.shippingUnits) {
                                                listOfShippingUnits.add(sUnit)
                                            }
                                        }
                                        Column(
                                            modifier = Modifier.wrapContentHeight().weight(1f).fillMaxWidth()
                                                .padding(horizontal = 8.dp)
                                        ) {
                                            for (n in 1..8) {
                                                orderShipped(
                                                    detailedCompositionModel,
                                                    n,
                                                    listOfShippingUnits
                                                )
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

Simply change state.detailedOrder!! in second case inside items block to order fix the case. Maybe someone will be such heedless as me, and this snippet will help.

Tomato answered 5/9 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.