24
votes

After updating from compose alpha-11 to alpha-12(or beta-01) I am getting this crash whenever I open an activity or fragment that has compose views.

I am using AppCompatActivity which implements LifecycleOwner, so this is extremely odd.

    java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@2da7146[MyActivity]
            at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:214)
            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:151)
            at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:199)
            at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:176)
            at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:207)
            at android.view.View.dispatchAttachedToWindow(View.java:20014)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3589)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3596)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3596)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3596)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3596)
            at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3596)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2223)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1888)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8511)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
            at android.view.Choreographer.doCallbacks(Choreographer.java:761)
            at android.view.Choreographer.doFrame(Choreographer.java:696)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
            at android.os.Handler.handleCallback(Handler.java:873)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:214)
            at android.app.ActivityThread.main(ActivityThread.java:7050)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

My code looks really simple:

    class MyActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            setContent {
                MaterialTheme {
                    Text(text = "compose")
                }
            }
        }
    }

UPDATE

Apparently you need to use androidx.appcompat:appcompat:1.3.0-beta01

6
Apparently you need to use androidx.appcompat:appcompat:1.3.0-beta01 - Clapa Lucian
I still have the same issue with BottomSheetDialogFragment. Updating to version 1.3.0-beta01 didn't help. Do you know where the problem lies? - lbasek
@Ibasek Probably related with this bug issuetracker.google.com/issues/180691023 - Clapa Lucian
@ClapaLucian solution worked for me. Essentialy adding implementation "androidx.fragment:fragment-ktx:1.4.0-SNAPSHOT" to your module build.gradle and add maven { url 'https://androidx.dev/snapshots/builds/7166224/artifacts/repository' } to the list of repositories - sachadso

6 Answers

11
votes

Try updating the dependency of AppCompat to rc01 version. This solved the problem for me.

implementation 'androidx.appcompat:appcompat:1.3.0-rc01'

3
votes

I've encountered same issue with BottomSheetDialogFragment You have to upgrade fragment to 1.3.1

Thanks to @clapa-lucian, you can find more about in this issue

3
votes

Switching from AppCompatActivity to FragmentActivity solved this issue in my case.

2
votes

As none of the solutions worked for me, I'm here to make your day easier (assuming you have the configuration that I had for my project).

So, here was the activity that didn't launch after upgrading to beta01:

class AuthenticationActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportFragmentManager.beginTransaction()
            .replace(android.R.id.content, SignInFragment())
            .commit()
    }
}

As you can see, there is not setContentView(...) in here. After analyzing the stacktrace I saw that setTag(R.id.view_tree_lifecycle_owner, lifecycleOwner) wasn't getting performed which resulted in getTag() to return null - hence exception.

Turns out setTag(...) is getting called when any of setContentView() overloads is performed.

So, the easy fix for my setup was to introduce a redundant setContentView(View(this)) which internally would set the lifecycle owner:

class AuthenticationActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(View(this)) // <-- here's the newly introduced line
        supportFragmentManager.beginTransaction()
            .replace(android.R.id.content, SignInFragment())
            .commit()
    }
}
0
votes

Upgrading androidx.appcompat:appcompat from 1.2.0 to 1.3.1 solved the issue for me.

TLDR: Update

implementation "androidx.appcompat:appcompat:1.2.0"

to

implementation "androidx.appcompat:appcompat:1.3.1"
0
votes

For me it was because I had not included the appcompat library and my activity inherited from Activity instead of AppCompatActivity. The problem resolved by adding the library:

implementation("androidx.appcompat:appcompat:1.3.0")

and subclassing from AppCompatActivity:

class MyActivity: AppCompatActivity() {
  ...
}