Jetpack Compose error on LazyColumn TextStyle cannot be cast to androidx.compose.runtime.RecomposeScopeImpl

Viewed 852

I'm exploring Jetpack Compose with some sample code found on here, however it keeps crashing whilst scrolling up and down. Sometimes it doesn't crash at all, but it seems to happen more if you scroll very quickly.

I am currently using compose version 1.0.0-rc01 but also tried compose 1.0.0-Beta09 with the same results. Would appreciate any insights as to what's happening. Whilst I appreciate it's early days I do think compose looks promising. Thanks in advance.

This is the error:

2021-07-14 17:33:44.532 23927-23927/co.uk.learncompose E/AndroidRuntime: FATAL EXCEPTION: main
    Process: co.uk.learncompose, PID: 23927
    java.lang.ClassCastException: androidx.compose.ui.text.TextStyle cannot be cast to androidx.compose.runtime.RecomposeScopeImpl
        at androidx.compose.runtime.ComposerImpl.addRecomposeScope(Composer.kt:2447)
        at androidx.compose.runtime.ComposerImpl.startRestartGroup(Composer.kt:2435)
        at androidx.compose.material.TextKt.Text-fLXpl1I(Text.kt:108)
        at co.uk.learncompose.MainActivityKt$LazyColumnDemo$1$invoke$$inlined$items$default$2.invoke(LazyDsl.kt:364)
        at co.uk.learncompose.MainActivityKt$LazyColumnDemo$1$invoke$$inlined$items$default$2.invoke(LazyDsl.kt:103)

Here's the code:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            LearnComposeTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.primaryVariant) {
                    Greeting("Android")
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String) {

    Row() {
        Column(modifier = Modifier.padding(16.dp)) {
            Text(
                text = "Hello $name!",
                modifier = Modifier.padding(bottom = 8.dp).fillMaxWidth(),
                style = MaterialTheme.typography.h5
            )
            LazyColumnDemo()
        }
    }
}

@Composable
fun LazyColumnDemo() {
    val list = listOf(
        "A", "B", "C", "D"
    ) + ((0..100).map { it.toString() })
    LazyColumn(modifier = Modifier.fillMaxHeight()) {
        items(items = list, itemContent = { item ->
            when (item) {
                "A" -> {
                    Text(text = item, style = TextStyle(fontSize = 80.sp))
                }
                "B" -> {
                    Button(onClick = {
                    }) {
                        Text(text = item, style = TextStyle(fontSize = 80.sp))

                    }
                }
                "C" -> {
                    //Do Nothing
                }
                "D" -> {
                    Text(text = item)
                }
                else -> {
                    Text(text = item, style = TextStyle(fontSize = 80.sp))
                }
            }
        })
    }
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    LearnComposeTheme {
        Greeting("Android")
    }
}
1 Answers

I too encountered this with the same demo code, running on compose 1.1.0-alpha05. I would also like to know why it crashes as-is, and not all the time.

One way I've gotten around the crash is to simply supply a style to item "D" Text. At least, I've not been able to reproduce the crash after supplying it, even if I supply the default that should be used if nothing is passed, and even if I use a different font size:

Using Default:

"D" -> {
    Text(text = item, style = LocalTextStyle.current)
}

Or different font size:

"D" -> {
    Text(text = item, style = TextStyle(fontSize = 12.sp))
}

Not an ideal answer, but hopefully a temporary workaround.

Related