Cla*_*ian 22 android android-view android-activity kotlin android-jetpack-compose
从撰写更新alpha-11到alpha-12(或beta-01)后,每当我打开具有撰写视图的活动或片段时,我都会遇到此崩溃。
我正在使用AppCompatActivitywhich implements 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)
Run Code Online (Sandbox Code Playgroud)
我的代码看起来很简单:
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Text(text = "compose")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新
显然你需要使用 androidx.appcompat:appcompat:1.3.0-beta01
如果你的视图是旧的,在我的例子中它是对话框,你可以尝试添加这样的行来手动设置视图树所有者
val decorView: View = dialog?.window?.decorView ?: throw IllegalStateException("Failed to get decorview")
ViewTreeLifecycleOwner.set(decorView, this)
ViewTreeViewModelStoreOwner.set(decorView, this)
view.setViewTreeSavedStateRegistryOwner(decorView.findViewTreeSavedStateRegistryOwner())
Run Code Online (Sandbox Code Playgroud)
以这种方式 DialogFragment 被修复在这里:
由于没有一个解决方案对我有用,我来这里是为了让您的一天更轻松(假设您拥有我为我的项目所做的配置)。
因此,这是升级到后未启动的活动beta01:
class AuthenticationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction()
.replace(android.R.id.content, SignInFragment())
.commit()
}
}
Run Code Online (Sandbox Code Playgroud)
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()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2080 次 |
| 最近记录: |