本文介绍了带有 Jetpack Compose 的键盘被 Compose beta-01 损坏(从 DecorView 中找不到 ViewTreeLifecycleOwner)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与这个问题有关

此处所述,我能够使用 Jetpack Compose 创建键盘.现在,在我将项目从 alpha-11 升级到 alpha-12 后,代码损坏了(并且在 beta-01 中仍然损坏) 并且每当我尝试打开键盘时都会收到以下错误:

As described here I was able to create a keyboard with Jetpack Compose. Now after I upgrade the project from alpha-11 to alpha-12 the code broke (and is still broken in beta-01) and I get the following error whenever I try to open the keyboard:

 java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@aaf805f[InputMethod]
        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:20105)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3430)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2052)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1745)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7768)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:967)
        at android.view.Choreographer.doCallbacks(Choreographer.java:791)
        at android.view.Choreographer.doFrame(Choreographer.java:726)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:940)

这很奇怪,因为我已经设置了 ViewTreeLifecycleOwner 这里 并且在以前版本的 compose 中工作得很好,直到 alpha-12

That's very weird because I have set the ViewTreeLifecycleOwner here and worked perfectly fine in previous versions of compose until alpha-12

这个错误让我不知道什么坏了,我应该改变什么.也许你能帮我.

The error gives me no idea of what's broken and what I should change. Maybe you can help me.

在这里您可以找到可重现示例的所有代码

推荐答案

错误信息说:

未从 DecorView@aaf805f[InputMethod] 中找到 ViewTreeLifecycleOwner

Compose 会在整个 WindowDecorView 上设置 ViewTreeLifecycleOwner,而不是在您的 View 本身上设置.

Compose looks for a ViewTreeLifecycleOwner set on the DecorView of your whole Window, not the one set on your View itself.

因此,不要在返回的 ComposeKeyboardView 上设置 ViewTrees,而是使用 getWindow()getDecorView()代码>获取窗口的装饰视图.

Therefore instead of setting the ViewTrees on the ComposeKeyboardView you return, instead use the InputMethodService API of getWindow() with getDecorView() to get the DecorView of your window.

override fun onCreateInputView(): View {
    val view = ComposeKeyboardView(this)
    window?.window?.decorView?.let { decorView ->
        ViewTreeLifecycleOwner.set(decorView, this)
        ViewTreeViewModelStoreOwner.set(decorView, this)
        ViewTreeSavedStateRegistryOwner.set(decorView, this)
    }
    return view
}

这篇关于带有 Jetpack Compose 的键盘被 Compose beta-01 损坏(从 DecorView 中找不到 ViewTreeLifecycleOwner)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:06