35

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

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Clapa Lucian
  • 590
  • 2
  • 7
  • 14
  • 4
    Apparently you need to use androidx.appcompat:appcompat:1.3.0-beta01 – Clapa Lucian Feb 26 '21 at 10:11
  • 1
    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 Feb 26 '21 at 17:02
  • 2
    @Ibasek Probably related with this bug https://issuetracker.google.com/issues/180691023 – Clapa Lucian Feb 26 '21 at 19:59
  • 2
    @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 Mar 02 '21 at 10:43

7 Answers7

18

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

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

4

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"
Stanislaw Baranski
  • 1,308
  • 9
  • 18
3

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()
    }
}
azizbekian
  • 60,783
  • 13
  • 169
  • 249
3

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

amrro
  • 1,526
  • 17
  • 21
3

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

Andrei Mesh
  • 256
  • 2
  • 20
  • 1
    This worked for me after trying many many many different suggestions. I wish I could give you more Up arrow. Thank you. – Musicdad May 22 '21 at 21:33
3

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() {
  ...
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
2

If your view is something old, in my case it was Dialog, you can try add such lines to set view tree owner manually

val decorView: View = dialog?.window?.decorView ?: throw IllegalStateException("Failed to get decorview")
        ViewTreeLifecycleOwner.set(decorView, this)
        ViewTreeViewModelStoreOwner.set(decorView, this)
        view.setViewTreeSavedStateRegistryOwner(decorView.findViewTreeSavedStateRegistryOwner())

In such way DialogFragment was fixed here:

https://android-review.googlesource.com/c/platform/frameworks/support/+/1610494/1/fragment/fragment/src/main/java/androidx/fragment/app/DialogFragment.java#48

Penzzz
  • 2,814
  • 17
  • 23
  • I have same problem with Dialog. Where should I add these lines to fix it? – faritowich May 26 '23 at 13:09
  • 1
    In Google's code they added it after dialog.show() in onStart // Only after we show does the dialog window actually return a decor view. – Penzzz May 26 '23 at 14:57