android - 当参数具有默认值时,为什么我不能使用导航组件将参数传递给 fragment ?

标签 android navigation arguments

我正在使用导航组件,但如果定义了参数,我不明白为什么将参数传递给下面的方法时会出错。我正在使用 SafeArgs,只有在为此参数定义默认值时才会出现此错误。
enter image description here
有人可以解释一下为什么会发生这种情况,我该如何解决?
这是导航图的一部分代码。

<fragment
        android:id="@+id/sleepTrackerFragment"
        android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
        android:label="fragment_sleep_tracker"
        tools:layout="@layout/fragment_sleep_tracker" >
        <action
            android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
            app:destination="@id/sleepQualityFragment" />
        <argument
            android:name="qualityLastSleep"
            app:argType="integer"
            android:defaultValue="-1" />
    </fragment>
    <fragment
        android:id="@+id/sleepQualityFragment"
        android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
        android:label="fragment_sleep_quality"
        tools:layout="@layout/fragment_sleep_quality" >
        <action
            android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
            app:destination="@id/sleepTrackerFragment" >
        </action>
    </fragment>

最佳答案

如文档中所述

Destination-level arguments and default values are used by all actions that navigate to the destination. If needed, you can override the default value of an argument (or set one if it doesn't already exist) by defining an argument at the action level. This argument must be of the same name and type as the argument declared in the destination.


因此您必须覆盖此值才能执行此操作,以便您可以遵循以下两种解决方案中的任何一种
解决方案一
 val action =  SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()
 action.qualityLastSleep = 5 // your value
 findNavController().navigate(action)

解决方案二
<fragment
    android:id="@+id/sleepTrackerFragment"
    android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
    android:label="fragment_sleep_tracker"
    tools:layout="@layout/fragment_sleep_tracker" >
   <argument
           android:name="qualityLastSleep"
           app:argType="integer"
           android:defaultValue="-1" />
    <action
        android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
        app:destination="@id/sleepQualityFragment" />
</fragment>
<fragment
    android:id="@+id/sleepQualityFragment"
    android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
    android:label="fragment_sleep_quality"
    tools:layout="@layout/fragment_sleep_quality" >
    
    <action
        android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
        app:destination="@id/sleepTrackerFragment" >
     <argument
           android:name="qualityLastSleep"
           app:argType="integer"
           android:defaultValue="-1" />
       
    </action>
</fragment>
然后你可以像这样调用你的导航
findNavController().navigate(SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment(5))

关于android - 当参数具有默认值时,为什么我不能使用导航组件将参数传递给 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63058373/

相关文章:

android - 通知服务在通知点击上增加了额外的费用,怎么样?

php - 可以使从数据库调用的图像成为另一个页面的链接吗?

java - -help 选项中的 Apache Commons CLI 多个参数值名称

c - 不兼容的指针类型错误 [C]

php - 中心导航栏,中心带有波峰( Logo )

r - 解析,用三点参数替换

java - 将 Drawable 转换为 BLOB 数据类型

c# - Xamarin MotionEventActions.Move 永远不会在自定义 View onTouch 事件中被命中?

android - 为 Android 中的 facebook 集成生成调试 key

html - 下推内容的透明导航菜单