Jetpack Compose Rendering Problem: ComposeView

Viewed 2040

I'm trying to use ComposeView in XML layout file and application works fine, but I'm getting rendering trouble in preview. If there's no way to fix this problem, can I just ignore somehow (via tools tag, for exmpl) this ComposeView at preview rendering? Main purpose - I want to see other XML elements, ComposeView is unnecessary (only for preview of course). Thank you

Android Studio Preview window shows the following problem:

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)

Simplified xml file, just for example

<?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>
0 Answers
Related