Mar*_*ina 8 navigation android arguments
我正在使用导航组件,但我不明白为什么如果定义了参数,则将参数传递给下面的方法时会出现错误。我正在使用 SafeArgs,只有当我为此参数定义默认值时才会出现此错误。
有人可以解释一下为什么会发生这种情况以及如何解决它吗?
这是导航图的部分代码。
<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>
Run Code Online (Sandbox Code Playgroud)
正如文档中提到的
导航到目标的所有操作都使用目标级别参数和默认值。如果需要,您可以通过在操作级别定义参数来覆盖参数的默认值(如果尚不存在则设置一个参数)。此参数必须与目标中声明的参数具有相同的名称和类型。
因此您必须覆盖此值才能执行以下任一解决方案
解决方案一
val action = SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()
action.qualityLastSleep = 5 // your value
findNavController().navigate(action)
Run Code Online (Sandbox Code Playgroud)
解决方案二
<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>
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样调用你的导航
findNavController().navigate(SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment(5))
Run Code Online (Sandbox Code Playgroud)
似乎您真正想要的是目的地级别参数:
请尝试以下操作:
<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" />
</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>
Run Code Online (Sandbox Code Playgroud)
您还可以按住 Ctrl 键并单击来actionSleepQualityFragmentToSleepTrackerFragment()检查生成的函数是否接受参数。
如果 IDE 抱怨参数传递,请尝试清理并重建
如果这对您不起作用,请告诉我
| 归档时间: |
|
| 查看次数: |
3126 次 |
| 最近记录: |