我目前正在为我的应用开发主屏幕,但我遇到了一个问题。val context = LocalContext.current
行导致了一个错误,该错误指出,“@Composable invocations can only happen from the context of a @Composable function”。您能否提供有关如何解决此问题的指导?为了给予更好的理解,以下是完整的函数:
@Composable
fun HomeScreen(navController: NavController) {
// Use a Box to allow for layering of composables on top of each other
// Used to keep the content at the bottom center of the screen.
Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp), // Apply padding around the box
contentAlignment = Alignment.BottomCenter // Align content to the bottom center
) {
// Column to stack buttons vertically
Column(
// Center column contents horizontally
horizontalAlignment = Alignment.CenterHorizontally,
// Space between the column contents
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// Button to NAVIGATE TO THE BENEFITS SCREEN
Button(
onClick = { navController.navigate("sleep_benefits") },
// Apply padding to only the top of the first button to push it upwards
modifier = Modifier.padding(bottom = 1.dp)
) {
Text(text = "Learn About Sleep Benefits")
}
// Button for USER LOGIN
Button(
onClick = { /* TODO: Add navigation to login screen here */ }
) {
Text(text = "Login to Track Sleep")
}
// New Sign Up Button
Button(
onClick = {
val context = LocalContext.current
context.startActivity(Intent(context, SignInActivity::class.java))
},
modifier = Modifier.padding(top = 8.dp)
) {
Text(text = "Sign Up")
}
}
}
}
字符串
我试着看这个:Error: "@Composable invocations can only happen from the context of a @Composable function"
1条答案
按热度按时间j1dl9f461#
替换:
字符串
使用:
型