jetpack compose java.lang.IllegalStateException:Start/end 不平衡

jetpack compose java.lang.IllegalStateException: Start/end imbalance

这段代码导致了这次崩溃:

我正在使用 compose 版本 1.0.0-alpha06

java.lang.IllegalStateException: Start/end imbalance   at androidx.compose.runtime.Composer.finalizeCompose(Composer.kt:2369)   at androidx.compose.runtime.Composer.endRoot(Composer.kt:575)   at androidx.compose.runtime.Composer.composeInitial(Composer.kt:2054)   at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:276)   at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:110)   at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:234)   at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.kt:-1)   at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.kt:627)   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)

有人可以帮助我吗?谢谢

@Composable
@Preview
private fun Image1() {
    Box(modifier = Modifier.fillMaxWidth().wrapContentHeight()) {
        Image(
                asset = imageResource(id = R.mipmap.fit_static_image_1),
                contentScale = ContentScale.FillWidth,
        )
        Column(Modifier.padding(start = 16.dp, end = 16.dp).align(Alignment.CenterStart), horizontalAlignment = Alignment.Start) {
            Text(
                    color = getColor(id = R.color.white),
                    fontWeight = FontWeight.Bold,
                    fontSize = TextUnit.Sp(18),
                    text = dicString(id = R.string.fit_static_image_1_title),
                    textAlign = TextAlign.Start
            )
            Text(
                    text = dicString(id = R.string.fit_static_image_1_description),
                    color = getColor(id = R.color.white),
                    fontSize = TextUnit.Sp(14),
                    modifier = Modifier.padding(top = 4.dp),
                    textAlign = TextAlign.Start
            )
        }
    }
}

我使用 remember { } 错误,我试图在 dicString 函数中使用它来记住获得的字符串。这导致了这个问题。我通过将资源 ID 作为参数添加到 remember 函数来解决这个问题。 记住( id ) { }

由于在可组合项的构造中缺少数据,从可组合项返回时我遇到了这种情况。

val dataOrNull by viewModel.data.collectAsState(null)

Box {
    MyComposable(
        data = dataOrNull ?: return
    )
}

我在构建树之前通过返回解决了这个问题

val dataOrNull by viewModel.data.collectAsState(null)

val data = dataOrNull ?: return

Box {
    MyComposable(
        data = data
    )
}

我在 compose 版本上遇到了同样的问题 compose_version = '1.1.0' 解决方法是从可组合函数中删除 return 语句。 示例:

@Composable
fun Foo() {
    if (true) return // convert to else statement
    Text(text = "foo")
}