Android Xml设计工具无法预览AbstractComposeView子类的小部件?

j13ufse2  于 2023-02-10  发布在  Android
关注(0)|答案(1)|浏览(119)

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?

42fyovps

42fyovps1#

我也遇到了同样的问题。我发现唯一有效的解决方法是去掉从AbstractComposeView继承的类,而使用不能从其继承的ComposeView。这意味着您必须将逻辑从StarLightBoardView类移到包含它的任何类中,然后在ComposeView对象上调用setContent

<LinearLayout
  ...

  <!-- Use ComposeView here rather than your Custom `StarLightBoardView` -->
  <androidx.compose.ui.platform.ComposeView
    android:id="@+id/myComposeView"
    ... />

/LinearLayout>

把你的逻辑

var starLight by mutableStateOf(0)

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

到包含类中

相关问题