ComposeView is not attached to window - Jetpack Compose
Asked Answered
B

1

14

I was trying to use FlexBox layout with JetpackCompose. I assumed that I can use normal RecyclerView and use ComposeView to render my view.

So I started with the documentation and attempted all the steps given in Compose document to use it with RecyclerView.

But once I did that, I got following error:

java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{83fefc1 V.E...... ......I. 0,0-0,0} is not attached to a window

[1] Adapter Code

class SelectAisleAdapter : BaseRecyclerViewAdapter<Any, SelectAisleAdapter.SelectAisleViewHolder>() {

    override fun getViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): SelectAisleViewHolder {
        return SelectAisleViewHolder(ComposeView(parent.context))
    }

    override fun onViewRecycled(holder: BaseViewHolder<Any>) {
        super.onViewRecycled(holder)
        if (holder is SelectAisleViewHolder) {
            holder.composeView.disposeComposition()
        }
    }

    inner class SelectAisleViewHolder(val composeView: ComposeView) : BaseViewHolder<Any>(composeView) {

        init {
            composeView.setViewCompositionStrategy(
                ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed
            )
        }

        override fun bind(item: Any) {
            composeView.setContent {
                AisleChip(text = "Milk", /*modifier = Modifier.padding(8.dp),*/ selected = false)
            }
        }
    }
}

[2] Compose Code

@Composable
fun AisleChip(
    modifier: Modifier = Modifier,
    text: String,
    selected: Boolean = false,
) {
    val backgroundColor = if (selected) colorResource(id = R.color.colorPrimary) else colorResource(id = R.color.colorBackground)
    val borderColor = if (selected) colorResource(id = R.color.colorPrimary) else colorResource(id = R.color.colorBorder)
    val textColor = if (selected) colorResource(id = R.color.white) else colorResource(id = R.color.colorBorder)

    Text(
        text = text,
        fontSize = 18.sp,
        fontWeight = FontWeight.Regular,
        fontFamily = appFontFamily,
        color = textColor,
        modifier = modifier
            .clip(RoundedCornerShape(8.dp))
            .border(
                width = 1.dp,
                color = borderColor,
                shape = RoundedCornerShape(8.dp)
            )
            .background(backgroundColor)
            .padding(16.dp)
    )
}

[3] Activity Code

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val rv = findViewById<RecyclerView>(R.id.rvMain)
        val adapter = SelectAisleAdapter()
        rv.layoutManager = FlexboxLayoutManager(this)
        rv.adapter = adapter
        adapter.replaceItems(listOf("1", "2", "3", "4", "5"))
    }

}

Complete StackTrace

    java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{83fefc1 V.E...... ......I. 0,0-0,0} is not attached to a window
        at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:225)
        at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:220)
        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:227)
        at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:264)
        at android.view.View.measure(View.java:24530)
        at com.google.android.flexbox.FlexboxHelper.calculateFlexLines(FlexboxHelper.java:477)
        at com.google.android.flexbox.FlexboxHelper.calculateHorizontalFlexLines(FlexboxHelper.java:249)
        at com.google.android.flexbox.FlexboxLayoutManager.updateFlexLines(FlexboxLayoutManager.java:960)
        at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:737)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
        at android.view.View.layout(View.java:21912)
        at android.view.ViewGroup.layout(ViewGroup.java:6260)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:725)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2021-09-28 16:14:51.363 21103-21103/com.alphastack.shoppy I/Process: Sending signal. PID: 21103 SIG: 9

Bertelli answered 28/9, 2021 at 10:53 Comment(2)
Were you able to fix the crash? I'm having the same problem in my app.Heartwhole
I am also facing same issueEnsphere
C
0

You are encountering that error because of this line:

rv.layoutManager = FlexboxLayoutManager(this)

It's a known issue, likely due to the way that the FlexboxLayoutManager implements onMeasure.

Try a different LayoutManager and see if it works.

Composed answered 7/5 at 1:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.