从撰写更新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 : …Run Code Online (Sandbox Code Playgroud) android android-view android-activity kotlin android-jetpack-compose
您可以在Github上找到一个示例项目来重现该问题
我一直在尝试使用 Jetpack Compose 作为键盘 UI。最终,当我尝试通过 InputMethodService 膨胀键盘时
class IMEService : InputMethodService() {
override fun onCreateInputView(): View = KeyboardView(this)
}
Run Code Online (Sandbox Code Playgroud)
通过使用这个视图
class KeyboardView(context: Context) : FrameLayout(context) {
init {
val view = ComposeView(context).apply {
setContent {
Keyboard() //<- This is the actual compose UI function
}
}
addView(view)
}
}
Run Code Online (Sandbox Code Playgroud)
或者
class KeyboardView2 constructor(
context: Context,
) : AbstractComposeView(context) {
@Composable
override fun Content() {
Keyboard()
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用键盘时,出现以下错误
class KeyboardView2 constructor(
context: Context,
) : AbstractComposeView(context) {
@Composable …Run Code Online (Sandbox Code Playgroud) android android-input-method android-view android-jetpack-compose
当我尝试使用 XML 在覆盖层中插入 Compose(在其他应用程序上绘制)时,出现以下异常:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}
Run Code Online (Sandbox Code Playgroud)
但如果没有覆盖(在活动中),它可以正常工作。有谁知道如何解决吗?我已经将 AppCompat 库更新到 1.3.0
我的 XML 代码:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{d596746 V.E...... ......ID 0,0-0,0}
Run Code Online (Sandbox Code Playgroud)
我的叠加代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<androidx.compose.ui.platform.ComposeView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/compose_view"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)