You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中MapsScreen无法跳转至SignupScreen问题排查

问题原因与修复方案

核心问题

你代码里的导航逻辑没有被执行——在AnnotatedClickableText的ClickableText回调中,你只是引用了onNavigateToSignupScreen函数,但没有调用它。run { onNavigateToSignupScreen }这种写法只是把函数作为表达式,不会触发实际的导航操作,这就是Logcat能打印点击日志,但页面不跳转的原因。

修复后的代码

修改AnnotatedClickableText中的ClickableText部分:

@Composable
fun AnnotatedClickableText(onNavigateToSignupScreen: () -> Unit) {
    val annotatedText = buildAnnotatedString {
        withStyle(
            style = SpanStyle(color = Color.Gray)
        ) {
            append("Don't have an account? ")
        }

        pushStringAnnotation(
            tag = "SignUp",
            annotation = "SignUp"
        )
        withStyle(
            style = SpanStyle(color = Color.Red)
        ) {
            append("Sign Up")
        }
        pop()
    }

    ClickableText(
        text = annotatedText,
        onClick = { offset ->
            // 先判断点击的是SignUp区域,避免无效点击
            annotatedText.getStringAnnotations(
                tag = "SignUp",
                start = offset,
                end = offset
            ).firstOrNull()?.let { annotation ->
                Log.d("Clicked", annotation.item)
                // 执行导航函数(关键:加括号调用)
                onNavigateToSignupScreen()
            }
        }
    )
}

关键修改点

  1. 调用导航函数:把onNavigateToSignupScreen改成onNavigateToSignupScreen(),触发导航操作
  2. 避免索引越界:用firstOrNull()替代[0],如果用户点击的是非SignUp文本区域,不会抛出数组越界异常
  3. 优化可读性:将参数名it改为offset,代码逻辑更清晰

额外检查项(可选)

如果修复后仍有问题,可以确认以下内容:

  • 确认Screens.SignUpScreen.route的路由字符串没有拼写错误
  • 确认SignUpScreen组件本身能正常渲染,没有崩溃或异常

内容的提问来源于stack exchange,提问作者Kaleab Woldemariam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:15:54