小编deo*_*ens的帖子

Jetpack Compose 渲染问题:ComposeView

我正在尝试在 XML 布局文件中使用 ComposeView 并且应用程序工作正常,但我在预览中遇到了渲染问题。如果没有办法解决这个问题,我可以在预览渲染时以某种方式(例如通过工具标签)忽略这个 ComposeView 吗?主要目的 - 我想查看其他 XML 元素,不需要 ComposeView(当然仅用于预览)。谢谢

Android Studio Preview 窗口显示以下问题:

java.lang.IllegalStateException: ViewTreeLifecycleOwner is not present in this window. Use
ComponentActivity, FragmentActivity or AppCompatActivity to configure ViewTreeLifecycleOwner
automatically, or call ViewTreeLifecycleOwner.set() for this View or an ancestor in the same
window.   
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.kt:176)
   at android.view.View.dispatchAttachedToWindow(View.java:20479)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
   at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
   at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
Run Code Online (Sandbox Code Playgroud)

简化的xml文件,仅举个例子

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".presentation.home.HomeActivity">
        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I want …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-studio android-jetpack android-jetpack-compose

5
推荐指数
0
解决办法
1352
查看次数