从 DecorView@2da7146[MyActivity] 中找不到 ViewTreeLifecycleOwner

问题描述 投票:0回答:8

从撰写

alpha-11
更新到
alpha-12
(或
beta-01
)后,每当我打开具有撰写视图的活动或片段时,我都会遇到此崩溃。

我正在使用

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

android kotlin android-activity android-view android-jetpack-compose
8个回答
18
投票

尝试将

AppCompat
的依赖项更新为
rc01
版本。 这为我解决了问题。

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


4
投票

androidx.appcompat:appcompat
1.2.0
升级到
1.3.1
解决了我的问题。

TLDR: 更新

implementation "androidx.appcompat:appcompat:1.2.0"

implementation "androidx.appcompat:appcompat:1.3.1"

4
投票

如果你的观点是旧的,就我而言,它是对话, 您可以尝试添加此类行来手动设置视图树所有者

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


3
投票

由于没有一个解决方案对我有用,我来这里是为了让您的一天更轻松(假设您拥有我为我的项目所做的配置)。

所以,这是升级到

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 - 因此出现异常。

事实证明,当执行任何

setTag(...)
重载时,
setContentView()
就会被调用。

因此,我的设置的简单修复方法是引入一个冗余的

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

3
投票

我遇到了同样的问题

BottomSheetDialogFragment
您必须将
fragment
升级为
1.3.1

感谢@clapa-lucian,您可以在这个issue

中找到更多相关信息

3
投票

AppCompatActivity
切换到
FragmentActivity
解决了我的问题。


3
投票

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

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

以及从 AppCompatActivity 进行子类化:

class MyActivity: AppCompatActivity() {
  ...
}

0
投票

@Penzzz 以上建议有效!需要注意的一件事是,您必须提供

LifecycleOwner
SavedStateRegistryOwner
ViewModelStoreOwner
的实现。这些相当简单。

我确实遇到了以下错误:

java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component

通过在

onCreate()
函数中调用 savingRegistryOwner.restore(savedInstanceState) 以及
onSavedInstanceState
onRestoreInstanceState
中的相应调用来修复此问题。

override fun onCreate(savedInstanceState: Bundle?) {
  savedRegistryOwnerImpl.restore(savedInstanceState)
  super.onCreate(savedInstanceState)
  lifeCycleOwnerImpl.onCreate()
}

我通过研究

ComponentActivity
如何使用
SavedStateRegistry
发现了这一点。

© www.soinside.com 2019 - 2024. All rights reserved.