从一个 Android 屏幕导航到另一个屏幕时出现深层链接异常

时间:2021-05-17 02:30:06

标签: kotlin jetpack-compose-navigation

我在 MainActivity 中有这个:

my.plot <- function(.formula, .data) {
  outcome.term <- deparse(.formula[[2]])
  first.predictor.term <- .formula[[3]]
  len <- length(first.predictor.term) > 1
  if (len) first.predictor.term <- .formula[[3]][[2]]
  if (len) .formula[[3]][[2]] <- substitute(new_variable)
  else .formula[[3]] <- substitute(new_variable)
  
  .data['new_variable'] <- 1:nrow(.data)
  
  my.fit <- lm(.formula, data = .data)
  my.predict <- predict(my.fit)
  f <- reformulate(deparse(first.predictor.term), outcome.term)
  plot(f, data = .data, ty = "p")
}

我的布局是这样定义的:

@Composable
fun createNavHost(paddingValues: PaddingValues, navController: NavHostController) {
    NavHost(navController, startDestination = Screen.HomeScreen.title) {
        composable(Screen.HomeScreen.title) {
            HomeScreen(
                accountSelected = {
                    navController.navigate(Screen.PasswordDetailScreen.title + "/${it.id}")
                })
        }
        composable(Screen.EditPasswordScreen.title) {
            EditPasswordScreen()
        }
    }
}

屏幕是这样定义的:

@Composable
fun MainLayout() {
    val navController = rememberNavController()

    PasswordHelperTheme {
        Scaffold(
        ) { paddingValues ->
            createNavHost(paddingValues, navController)
        }
    }
}

我打电话

@Composable 有趣的 AddAccountFAB() { val 上下文 = 本地上下文 val navController = rememberNavController()

sealed class Screen(val title: String) {
    object HomeScreen : Screen("Home")
    object SettingsScreen : Screen("Settings")
    object EditPasswordScreen : Screen("Edit")
    object PasswordDetailScreen: Screen("ArticleDetail")
}

并得到

ExtendedFloatingActionButton(
    shape = CircleShape,
    icon = { Icon(Icons.Filled.Add, "") },
    onClick = {
        navController.graph = navController.graph
        navController.navigate(MainActivity.Screen.EditPasswordScreen.title) {
            popUpTo = navController.graph.startDestination
            launchSingleTop = true
        }
    },
    elevation = FloatingActionButtonDefaults.elevation(8.dp),
    text = { Text("Add Account")}
)

我以为 java.lang.IllegalStateException: You must call setGraph() before calling getGraph() at androidx.navigation.NavController.getGraph(NavController.java:812) at com.blackfox.passwordhelper.android.HomeScreenKt$AddAccountFAB$1$1.invoke(HomeScreen.kt:79) 正在设置图表。这至少让我克服了深层链接异常。我只是想使用 FAB 转到页面。

2 个答案:

答案 0 :(得分:0)

尝试删除

navController.graph = navController.graph

答案 1 :(得分:0)

看来唯一真正的选择是在 route 中设置一个 navHost

NavHost(navController, startDestination = Screen.HomeScreen.title) {
        composable(Screen.HomeScreen.title) {
            HomeScreen(
                accountSelected = {
                    navController.navigate(Screen.PasswordDetailScreen.title + "/${it.id}")
                },
                fabSelected = {
                    navController.navigate(Screen.EditPasswordScreen.title + "/${it.id}")
                })
        }
        composable(Screen.EditPasswordScreen.title + "/-1") {
            EditPasswordScreen()
        }
    }

这是有效的:

ExtendedFloatingActionButton(
    shape = CircleShape,
    icon = { Icon(Icons.Filled.Add, "") },
    onClick = {
        fabSelected(Passwords(-1, "", "", "", "", ""))
    },
    elevation = FloatingActionButtonDefaults.elevation(8.dp),
    text = { Text("Add Account")}
)