Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

this piece of code cause this crash:

im using compose version 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$1.invoke(Wrapper.kt:234)   at androidx.compose.ui.platform.WrappedComposition$setContent$1.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)

can someone help me? thanks

@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
            )
        }
    }
}
like image 905
Kochchy Avatar asked Nov 18 '20 13:11

Kochchy


2 Answers

I had this happen when returning from a composable due to lack of data from within the construction of a composable.

val dataOrNull by viewModel.data.collectAsState(null)

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

I fixed this by returning prior to constructing the tree

val dataOrNull by viewModel.data.collectAsState(null)

val data = dataOrNull ?: return

Box {
    MyComposable(
        data = data
    )
}
like image 140
Tom Avatar answered Oct 19 '22 04:10

Tom


I experienced the same issue on compose version compose_version = '1.1.0' The fix was to remove the return statement from a composable function. Example:

@Composable
fun Foo() {
    if (true) return // convert to else statement
    Text(text = "foo")
}
like image 3
Jim Ovejera Avatar answered Oct 19 '22 04:10

Jim Ovejera