Luis Mierez

    Luis Mierez

    1 year ago
    I’m trying to figure out why I keep getting an error when trying to add compose to my project. Haven’t been able to find an answer from searching online, so maybe y’all can help me. I’ll post the stack trace in thread
    java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.coordinatorlayout.widget.CoordinatorLayout{96763ee V.E...... ......I. 0,0-0,0} at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:242) at androidx.compose.ui.platform.WindowRecomposer_androidKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:1) at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.android.kt:98) at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:153) at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:228) at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:200) at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:235) at android.view.View.dispatchAttachedToWindow(View.java:22010) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4291) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:4298) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3087) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2575) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9910) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1010) at android.view.Choreographer.doCallbacks(Choreographer.java:809) at android.view.Choreographer.doFrame(Choreographer.java:744) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:995) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8512) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
    I’m thinking is because of some dependency, but not sure which one and nothing I’ve tried has solved it
    also I’m trying to add a simple composable to an existing screen with a
    ComposeView
    in the xml
    i

    Ian Lake

    1 year ago
    If you're using
    AppCompatActivity
    , make sure you're using AppCompat
    1.3.0-rc01
    - only the 1.3 versions set the
    ViewTreeLifecycleOwner
    that
    ComposeView
    requires
    Luis Mierez

    Luis Mierez

    1 year ago
    thanks! I’ll try that. Is this documented anywhere? I haven’t been able to find which dependencies work with compose, so I’ve been a little lost
    l

    Louis Pullen-Freilich [G]

    1 year ago
    Compose should throw an exception telling you to use 1.3+, so I’m curious what code path triggers this - how are you adding
    ComposeView
    / how are you calling
    setContent
    on it?
    i

    Ian Lake

    1 year ago
    (Are you calling
    setContent
    on your
    ComposeView
    at all?)
    Luis Mierez

    Luis Mierez

    1 year ago
    The
    ComposeView
    I’m just adding in the xml, and then in
    Fragment
    I’m calling the
    setContent
    which passing in my composable function
    <androidx.compose.ui.platform.ComposeView
                android:id="@+id/user_section_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    updateInfoComposeView.setContent {
                UserInfoSection()
            }
    this is an older screen so just want to move a single section that displays some user info. I’m not even passing in any data and I’m just hardcoding everything in the composable until I get something to show up on the screen
    l

    Louis Pullen-Freilich [G]

    1 year ago
    Where in your
    Fragment
    are you calling
    setContent
    ?
    Luis Mierez

    Luis Mierez

    1 year ago
    in
    onViewCreated
    i

    Ian Lake

    1 year ago
    Then yeah, unless this was the very first fragment in your app, your
    ComposeView
    will have gone through
    onAttachedToWindow
    by that point
    Luis Mierez

    Luis Mierez

    1 year ago
    btw I was using an older version of appcompat, I never got any errors telling me that I was in the wrong version though. Updated and will let you know if that worked
    i

    Ian Lake

    1 year ago
    If you're in a Fragment, you'll need to upgrade your Fragment version to 1.3.X
    (for the same reason)
    Luis Mierez

    Luis Mierez

    1 year ago
    yeah that one I’m on 1.3.3, I missed the appcompat one
    i

    Ian Lake

    1 year ago
    I wonder if
    ComposeView
    could avoid throwing in
    onAttachedToWindow
    and instead wait for you call
    setContent
    for the first time? Then you'd get the good error message
    l

    Louis Pullen-Freilich [G]

    1 year ago
    Unless I’m missing something,
    ComposeView
    shouldn’t create anything until is
    setContent
    is called, no? Only
    AbstractComposeView
    allows for extra choices in terms of when to set up the composition
    Luis Mierez

    Luis Mierez

    1 year ago
    updating the appcompat version worked! thanks!
    Is there a place where which dependencies work with compose is documented?
    Luis Mierez

    Luis Mierez

    1 year ago
    gotcha. Not sure why I wasn’t getting that error though
    l

    Louis Pullen-Freilich [G]

    1 year ago
    If you have a small reproducible sample, it would be helpful if you could file a bug, since this is a very confusing crash when you don’t see this error message 🙂
    Luis Mierez

    Luis Mierez

    1 year ago
    not sure if I can come up with a reproducible sample, but I can compile a list of our apps dependencies (some of which are very old) and try to do something with that
    either way, updating appcompat worked. I appreciate all the help!