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