Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: replace() called on item that was not placed

Issue appears on multiple screens. To my surprise the same code was working for previous versions of app. I just updated the compose libraries and then all this happening again and again on multiple locations.

I am using LazyColumn to populate list items. Whenever I try to scroll it, the App crashes. Below is the abstract code, that I'm using. The issue appears after updating compose Bom version to 2023-08-00. The same code works for other screens. The only difference here is that the lazy column item is almost half of the screen height(1:1 aspect ratio). So mostly two items per screen.

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)
    }


    if (users.isNotEmpty()) {
        item {
            Spacer(modifier = Modifier.height(40.dp))
        }
    }

    composeLazyColumnPagingLoader(
        viewModel.listState,
        users,
        "No users available"
    )
}

Here is the Crash Log.

java.lang.IllegalStateException: replace() called on item that was not placed
                                        at androidx.compose.ui.node.LayoutNodeLayoutDelegate$LookaheadPassDelegate.replace(LayoutNodeLayoutDelegate.kt:1517)
                                        at androidx.compose.ui.node.LayoutNode.lookaheadReplace$ui_release(LayoutNode.kt:926)
                                        at androidx.compose.ui.node.MeasureAndLayoutDelegate.remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:471)
                                        at androidx.compose.ui.node.MeasureAndLayoutDelegate.access$remeasureAndRelayoutIfNeeded(MeasureAndLayoutDelegate.kt:39)
                                        at androidx.compose.ui.node.MeasureAndLayoutDelegate.measureAndLayout(MeasureAndLayoutDelegate.kt:352)
                                        at androidx.compose.ui.platform.AndroidComposeView.measureAndLayout(AndroidComposeView.android.kt:908)
                                        at androidx.compose.ui.node.Owner.measureAndLayout$default(Owner.kt:228)
                                        at androidx.compose.ui.platform.AndroidComposeView.dispatchDraw(AndroidComposeView.android.kt:1156)
                                        at android.view.View.draw(View.java:23269)
                                        at android.view.View.updateDisplayListIfDirty(View.java:22133)
                                        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4513)
                                        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4486)
                                        at android.view.View.updateDisplayListIfDirty(View.java:22089)
                                        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4513)
                                        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4486)
                                        at android.view.View.updateDisplayListIfDirty(View.java:22089)
                                        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4513)
                                        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4486)
                                        at android.view.View.updateDisplayListIfDirty(View.java:22089)
                                        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4513)
                                        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4486)
                                        at android.view.View.updateDisplayListIfDirty(View.java:22089)
                                        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:689)
                                        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:695)
                                        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:793)
                                        at android.view.ViewRootImpl.draw(ViewRootImpl.java:4789)
                                        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4500)
                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3687)
                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2371)
                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9297)
                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231)
                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
                                        at android.view.Choreographer.doCallbacks(Choreographer.java:899)
                                        at android.view.Choreographer.doFrame(Choreographer.java:832)
                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214)
                                        at android.os.Handler.handleCallback(Handler.java:942)
                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                        at android.os.Looper.loopOnce(Looper.java:201)
                                        at android.os.Looper.loop(Looper.java:288)
                                        at android.app.ActivityThread.main(ActivityThread.java:7918)
                                        at java.lang.reflect.Method.invoke(Native Method)
                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
like image 737
Abdullah Javed Avatar asked Oct 25 '25 16:10

Abdullah Javed


2 Answers

In my case the problem was using ConstraintLayout in LazyColumn. I didn't want to rewrite the layout and found another solution.

I updated androidx.constraintlayout:constraintlayout-compose to version 1.1.0-alpha13. This worked for me. I'm using compose_version:1.5.4.

like image 75
asprog_ii Avatar answered Oct 27 '25 06:10

asprog_ii


After launching the app multiples times and find manually what was causing the issue. i found the problem is Constraint Layout for compose in LazyList.

Every time my app faces any bug; I found the problem is somehow releated to Constriant Layout as people building it doesnt seem to care to update it with new compose.

I thinck people should avoid using Constraint layout unless it is actually stable

like image 39
Sheikh Zakir Ahmad Avatar answered Oct 27 '25 05:10

Sheikh Zakir Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!