'Android Xml Design Tool Cannot Preview The Widget of AbstractComposeView's Child Class?

This is my code, a class of AbstractComposeView. But it cannot show on the xml design tool. And The xml design tool shows the error message:

java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.constraintlayout.widget.ConstraintLayout{3deb8822 V.E...... ......ID 0,0-0,0} at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:244) 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:99) at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:155) at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:230) at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:244) at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:251) at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:283) at android.view.View.dispatchAttachedToWindow(View.java:20753) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3490) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497) at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:57) at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:368) at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436) at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121) at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:736) at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:892) at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)

class StarLightBoardView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AbstractComposeView(
    context,
    attrs,
    defStyleAttr
) {

    var starLight by mutableStateOf(0)

    var onAddStarClick by mutableStateOf<() -> Unit>({})

    override var shouldCreateCompositionOnAttachedToWindow: Boolean = false
        private set

    init {
        shouldCreateCompositionOnAttachedToWindow = true
    }

    @Composable
    override fun Content() {
        StarLightBoardView(
            modifier = Modifier.size(R.dimen.dp_113, R.dimen.dp_30),
            starLight = if (isInEditMode) 10 else starLight,
            typeface = when {
                isInEditMode -> Typeface.DEFAULT_BOLD
                else -> AppGlobalConfig.getNumberFontTypeface(context)
            },
            onAddStarClick = if (isInEditMode) fun() {} else onAddStarClick
        )
    }
}

Thanks for your answer, can you help me to resolve the problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source