【问题标题】:NavDirections arguments in wrong orderNavDirections 参数顺序错误
【发布时间】:2021-12-27 03:49:55
【问题描述】:

我正在使用 Android Jetpack Navigation 组件,它似乎要求我以错误的顺序传递参数。

我有两个片段(片段 1 和片段 2)。片段 2 有四个参数(bool、string、int、CustomParslableClass?)。 IDE 认为这是正确的顺序(如果我没有按该顺序显示红色曲线并显示消息“类型不匹配 [...]”)。该应用程序使用按照该顺序构建良好(一年前我最后一次工作时),但今天我下载了所有内容并且必须更新依赖项才能构建。

这是IDE认为正确的正常代码行(用于工作):

val direction = HomeViewPagerFragmentDirections.actionFragment1ToFragment2(false, "Favorites", 17, null)

但是,当我使用该顺序构建时,每个参数都会出现构建错误:

e: filename.kt: (51, 106): The boolean literal does not conform to the expected type String
e: filename.kt: (51, 112): Type mismatch: inferred type is String but Int was expected
e: filename.kt: (51, 125): Type mismatch: inferred type is Int but PlaylistEntry? was expected
e: filename.kt: (51, 136): Null can not be a value of a non-null type Boolean

相反,当我像这样 (arg2, arg3, arg4, arg1) 重新排序函数的参数时:

val direction = HomeViewPagerFragmentDirections.actionFragment1ToFragment2("Favorites", 17, null, false)

它构建良好,但 IDE 在每个参数下显示红色下划线,表示它们的顺序错误! (Type mismatch. Required: Boolean, Found: String)


也许很重要:

  • arg1 (bool) 的默认值为 false
  • arg2(字符串)没有默认值
  • arg3 (int) 没有默认值
  • arg4 (CustomParslableClass?) 没有默认值(但可以为空)

当我将 arg2 更改为具有默认值时,IDE 期望相同,但适用于构建的顺序更改为 (arg3, arg4, arg1, arg2)


简单的清理和重建没有帮助。我还尝试清理项目,关闭 Android Studio 并重新启动,然后进行构建。我什至尝试删除两个片段之间的动作,然后重新创建(在时间之间构建)。一切都无济于事。请注意,当我运行构建的应用程序时,一切正常正常情况下会很好,但我认为参数顺序不应该像那样跳跃!

【问题讨论】:

    标签: android-studio kotlin android-jetpack android-jetpack-navigation android-navigation-graph


    【解决方案1】:

    使用多参数你应该像下面这样设置

    val action = BlankFragmentDirections.actionBlankFragmentToSecondFragment()
            action.arg1 = 2
            action.arg2 = 3
            action.arg3 = "423"
            findNavController().navigate(action)
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2017-11-14
      • 1970-01-01
      • 2021-08-04
      • 2014-02-20
      相关资源
      最近更新 更多