Kochchy
Kochchy

Reputation: 788

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
            )
        }
    }
}

Upvotes: 21

Views: 12443

Answers (6)

sayang_android
sayang_android

Reputation: 304

If anyone is facing this in the future, another issue is having continue inside loop in Composable such as :

for (direction in MainRoute.values()) {
    if (direction == MainRoute.MAIN_MENU) continue
    Button(onClick = { navController.navigate(direction.route) }) {
        Text(text = direction.title)
    }
}

Upvotes: 1

Jim Ovejera
Jim Ovejera

Reputation: 876

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 if-else statement instead of return
    Text(text = "foo")
}

Upvotes: 6

shubham chouhan
shubham chouhan

Reputation: 991

My use case -

Was calling return from a if check inside BoxScope

Box() {
if (state.value == false) return

//doSomething()
}

replacing return with return@Box solved this error

Box() {
if (state.value == false) return@Box

//doSomething()
}

Upvotes: 1

Merkost
Merkost

Reputation: 2376

I my case with Koin Dependency Injection replacing this:

val viewModel: MyViewModel by viewModel()

by this:

val viewModel: MyViewModel = getViewModel()

resolved my error.

Hope it helps someone!

Upvotes: 1

Tom
Tom

Reputation: 7804

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
    )
}

Upvotes: 28

Kochchy
Kochchy

Reputation: 788

i was using remember { } wrong, i was trying to use it in dicString function to remember obtained string. That caused the issue. I fixed that by adding the resource id to the remember function as parameter. remember( id ) { }

Upvotes: 2

Related Questions