简体   繁体   English

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

[英]Why can't I pass an argument to fragment using navigation component when that argument has a default value?

I am using navigation component and I don't understand why I get an error passing the argument to the method below if the argument is defined.我正在使用导航组件,但如果定义了参数,我不明白为什么将参数传递给下面的方法时会出错。 I am using SafeArgs and I only get this error when I define a default value for this argument.我正在使用 SafeArgs,只有在为此参数定义默认值时才会出现此错误。

在此处输入图像描述

Could someone explain me why this happens and how can I solve it?有人可以解释一下为什么会发生这种情况,我该如何解决?

Here is a part of the code of the navigation graph.这是导航图的一部分代码。

<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>

seems like what you really want is destination level argument:似乎您真正想要的是目标级别的参数:

Try the following:尝试以下操作:

<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>

You can also Ctrl+Click on actionSleepQualityFragmentToSleepTrackerFragment() to check if the generated function accepts an argument.您也可以 Ctrl+单击actionSleepQualityFragmentToSleepTrackerFragment()以检查生成的 function 是否接受参数。

Do try clean and rebuild if IDE complains about argument passing如果 IDE 抱怨参数传递,请尝试清理并重建

Doc link: https://developer.android.com/guide/navigation/navigation-pass-data#override_a_destination_argument_in_an_action文档链接: https://developer.android.com/guide/navigation/navigation-pass-data#override_a_destination_argument_in_an_action

Let me know if this does not work for you让我知道这是否不适合您

As mentioned in documentation如文档中所述

Destination-level arguments and default values are used by all actions that navigate to the destination.目标级别 arguments 和默认值由导航到目标的所有操作使用。 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.此参数必须与目标中声明的参数具有相同的名称和类型。

so you have to override this value to do so you can follow any of the both solutions below因此您必须覆盖此值才能执行此操作,以便您可以遵循以下两种解决方案中的任何一种

solution one解决方案一

 val action =  SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment()
 action.qualityLastSleep = 5 // your value
 findNavController().navigate(action)

solution two解决方案二

<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>

and then you can call your navigation like this然后你可以像这样调用你的导航

findNavController().navigate(SleepQualityFragmentDirections.actionSleepQualityFragmentToSleepTrackerFragment(5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用导航组件安全参数将列表作为参数传递给片段 - How to pass a List as an argument to a Fragment using Navigation Component safeargs 如何将 function 作为参数传递给目标片段 - Android 导航组件 - How to pass a function as an argument to a destination fragment - Android Navigation Component 使用导航组件安全参数将枚举作为参数传递给片段的正确方法是什么 - What is the correct way to pass an Enum as an argument to a Fragment using Navigation Component safeargs 为什么我不能通过导航组件中的全局操作传递 arguments? - Why I can´t pass arguments with global action in navigation component? 使用导航组件时,如何在运行时为通过导航视图菜单访问的目的地设置参数? - When using Navigation Component, how do I set an argument at runtime for a destination accessed via navigation view menu? 使用高阶函数时无法传递参数 - I can't pass the argument while using higher order function 如何在 android 导航组件中传递参数名称? - How to pass argument name in android navigation component? 为什么优先使用默认值参数 - why there is default value argument in preference Jetpack Navigation 中操作的默认参数值 - Default argument value for action in Jetpack Navigation 我无法在 ViewModelProviders.of() function 中将片段作为参数 - I can't give fragment as argument inside ViewModelProviders.of() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM