android 在DecorView@2da7146[我的活动]中找不到视图树生命周期所有者

dw1jzc5e  于 2023-01-15  发布在  Android
关注(0)|答案(7)|浏览(130)

从compose alpha-11更新到alpha-12(或beta-01)后,每当我打开具有compose视图的Activity或片段时,都会出现此崩溃。
我使用的是AppCompatActivity,它实现了LifecycleOwner,所以这非常奇怪。

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)

我的代码看起来非常简单:

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

显然,您需要使用androidx.appcompat:appcompat:1.3.0-beta01

dluptydi

dluptydi1#

尝试将AppCompat的依赖项更新为rc01版本。这为我解决了问题。
implementation 'androidx.appcompat:appcompat:1.3.0-rc01'

kgqe7b3p

kgqe7b3p2#

androidx.appcompat:appcompat1.2.0升级到1.3.1为我解决了这个问题。

**TLDR:**更新

implementation "androidx.appcompat:appcompat:1.2.0"

implementation "androidx.appcompat:appcompat:1.3.1"
k10s72fa

k10s72fa3#

由于没有一个解决方案对我有效,我在这里让您的工作更轻松(假设您有我为我的项目所做的配置)。
下面是升级到beta01后未启动的活动:

class AuthenticationActivity : AppCompatActivity() {

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

正如你所看到的,这里没有setContentView(...),在分析了堆栈跟踪之后,我发现setTag(R.id.view_tree_lifecycle_owner, lifecycleOwner)没有被执行,这导致getTag()返回null -因此出现异常。
结果表明,当执行任何setContentView()重载时,setTag(...)都会被调用。
因此,对我的设置的简单修复是引入一个冗余的setContentView(View(this)),它将在内部设置生命周期所有者:

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()
    }
}
vd2z7a6w

vd2z7a6w4#

我在使用BottomSheetDialogFragment时遇到了相同的问题,您必须将fragment升级到1.3.1
感谢@clapa-lucian,您可以在此issue中找到有关的更多信息

bvhaajcl

bvhaajcl5#

AppCompatActivity切换到FragmentActivity在我的情况下解决了这个问题。

c9x0cxw0

c9x0cxw06#

对我来说,这是因为我没有包含 appcompat 库,而且我的Activity继承自 Activity 而不是 AppCompatActivity。通过添加库解决了问题:

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

并从 AppCompatActivity 生成子类:

class MyActivity: AppCompatActivity() {
  ...
}
yptwkmov

yptwkmov7#

如果您的视图是旧的,在我的例子中是Dialog,您可以尝试添加这样的行来手动设置视图树所有者

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

以这种方式DialogFragment被修复在这里:
https://android-review.googlesource.com/c/platform/frameworks/support/+/1610494/1/fragment/fragment/src/main/java/androidx/fragment/app/DialogFragment.java#48

相关问题