带有 Jetpack Compose 的键盘被 Compose beta-01 损坏(从 DecorView 中找不到 ViewTreeLifecycleOwner)

时间:2021-03-02 19:41:32

标签: android android-jetpack-compose

This is related to this question

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

 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 here 并且在以前版本的 compose 中工作得非常好,直到 alpha-12

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

Here you can find all code for a reproducible example

1 个答案:

答案 0 :(得分:4)

错误信息说:

<块引用>

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

Compose 在整个 ViewTreeLifecycleOwnerDecorView 上寻找一个 Window 集合,而不是在您的视图本身上寻找一个集合。

因此,不要在返回的 ViewTree 上设置 ComposeKeyboardView,而是使用 getWindow()InputMethodService API 和 getDecorView() 来获取你的窗户。

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
}