k

    Ky

    1 year ago
    Has anyone had issues with
    ComponentActivity.setContent
    after updating to alpha12? on alpha11, something like this worked fine
    someFrameLayout.setContent(Recomposer.current()) {
                CommonMdcTheme {
                    SomeComposeView(viewModel)
                }
            }
    After updating to alpha 12, it complains about receiver mismatch. I’ve tried both deprecated method from
    androidx.compose.ui.platform.setContent
    and new method
    androidx.activity.compose.setContent
    manueldidonna

    manueldidonna

    1 year ago
    You're invoking setContent with a ViewGroup as receiver, this extension has been removed
    k

    Ky

    1 year ago
    I didn’t see that they removed it in the changes notes. Thanks!
    i

    Ian Lake

    1 year ago
    It is a bit hidden -
    ViewGroup.setContent
    was replaced with ComposeView: https://android-review.googlesource.com/c/platform/frameworks/support/+/1497121
    So you'd use:
    val view = ComposeView(context)
    view.setContent {
      CommonMdcTheme {
        // ...
      }
    }
    someFrameLayout.addView(view)
    k

    Ky

    1 year ago
    Yeah that makes sense, so weird though I just double checked and it was working on my build with alpha11. Maybe something to do with AS caching 🤷
    Yeah, you're not the only one that was hit with a lot of API churn in alpha12. With beta (and API stability) around the corner, this will be a growing pain of the past soon enough 🙂
    k

    Ky

    1 year ago
    I’ve encountered another snag, most likely some weird thing I’m doing but I’ll post here just in case.
    NullPointerException: Attempt to invoke interface method 'void androidx.compose.runtime.MutableState.setValue(java.lang.Object)' on a null object reference
    This happens after calling
    class Activity {
    
    fun onCreate() {
            val rootView: FrameLayout = findViewById(R.id.rootView)
            val composeView = ComposeView(context = this)
            Timber.d("Activity: setting view: $view")
            view.setContent {
                CommonMdcTheme {
                    CustomComposeView(viewModel)
                }
            }
            rootView.addView(composeView)
      }
     }
    }
    From debugging, it hits
    androidx.compose.ui.platform.Wrapper.kt
    and the error is thrown in
    internal fun ViewGroup.setContent(
        parent: CompositionContext,
        content: @Composable () -> Unit
    ): Composition
    Specifically when it calls
    AndroidComposeView(context).also { addView(it.view, DefaultLayoutParams)
    and the context is from my Activity.
    i

    Ian Lake

    1 year ago
    Probably best to file an issue with a sample project, since that code doesn't compile or have the relevant bits needed to figure out what is going on
    I'd just make sure you're using the right base class -
    ComponentActivity
    or one of its subclasses - and using the latest version of those dependencies
    k

    Ky

    1 year ago
    Will do 👍
    Mehdi Haghgoo

    Mehdi Haghgoo

    1 year ago
    Does beta coming out mean we will not see breaking changes?
    Colton Idle

    Colton Idle

    1 year ago
    @Ky did you ever figure out your issue? By chance do you use buildSrc and/or build.gradle.kts?
    Ian, I'm hitting the same issue but my project is upgrading from alpha09 to beta01. This project is really a really huge existing project, BUT I was able to make it work in compose in alpha09 displaying a demo "Hello World" Text composable in a fragment. After updating to beta01 this fragment now crashes at runtime with the same stack trace as above
    NullPointerException: Attempt to invoke interface method 'void androidx.compose.runtime.MutableState.setValue(java.lang.Object)' on a null object reference
    I went through and made sure all of my deps are updated as well as my fragment and activity deps. They look fine, but I still get a crash in my fragment. For the heck of it, I changed my Activity to instead setContent { Text("hellow world") } and it worked! This leads me to believe something with fragments isn't working correctly. It builds and install, but crashes at runtime. Any ideas?
    i

    Ian Lake

    1 year ago
    File a bug against Jetpack Compose, just like what was recommended above
    Colton Idle

    Colton Idle

    1 year ago
    @Ian Lake 👍 I would have done so already but I can't reproduce in a new project, only happens in this existing project.
    i

    Ian Lake

    1 year ago
    Well, you can either try adding more stuff to your sample project until it starts crashing or keep removing stuff from your existing project to see when the crash goes away.
    I'd just make sure your non-Compose dependencies are the same first - use Fragment 1.3.0, AppCompat 1.3.0-beta01, etc.
    Colton Idle

    Colton Idle

    1 year ago
    Yep will try this morning. Just way curious if an activity with compose working, but not a fragment rang any bells for you. Thanks Ian
    I'll start there. Thanks
    For anyone else following along. I went through and audited all of my deps. The only one that was out of date was appcompat. Updated to the latest, but I still get the same issue. I'm going to continue pulling things apart, but let me know if anyone makes some progress.
    Alright, spent most of the day unfortunately debugging and I wasn't able to get to a minimal repro project but I filed this bug just in case anyone wants to star it https://issuetracker.google.com/issues/181463117
    k

    Ky

    1 year ago
    @Colton Idle Thanks for filing that bug, I never resolved my issue so i’ve starred your report. My case actually happened in a fragment and I did not use data binding. I’ll be following your bug report