LazyColumn TextStyle 上的 Jetpack Compose 错误无法转换为 androidx.compose.runtime.RecomposeScopeImpl

时间:2021-07-14 16:52:07

标签: android android-jetpack android-jetpack-compose

我正在使用此处找到的一些示例代码探索 Jetpack Compose,但是它在上下滚动时一直崩溃。有时它根本不会崩溃,但如果您快速滚动,它似乎发生得更多。

我目前正在使用 compose 版本 1.0.0-rc01,但也尝试过 compose 1.0.0-Beta09,结果相同。希望对发生的事情有任何见解。虽然我很欣赏它的早期,但我确实认为 compose 看起来很有希望。提前致谢。

这是错误:

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)

代码如下:

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

0 个答案:

没有答案