我正在尝试在XML布局文件中使用ComposeView,并且应用程序运行良好,但是在预览中出现了渲染问题。如果无法解决此问题,我是否可以在预览渲染时以某种方式(例如通过tools标记)忽略此ComposeView?主要目的-我想查看其他XML元素,ComposeView是不必要的(当然仅用于预览)。谢谢
Android Studio预览窗口显示以下问题:
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)
以简化的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 to see at least this text on preview"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/hello_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.compose.ui.platform.ComposeView
android:id="@+id/hello_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_view"
tools:context=".presentation.home.HomeActivity">
</androidx.compose.ui.platform.ComposeView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>