当我将相同的片段添加到第二个导航图时,不识别动作类 [英] When I add the same fragment to second navigation graph, don´t recognize the Action Class

查看:9
本文介绍了当我将相同的片段添加到第二个导航图时,不识别动作类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将相同的片段添加到不同的导航图中.这是第一张图的一段代码 (mobile_navigation)

I need to add the same fragment to different navigation graphs. This is piece of code of first graph (mobile_navigation)

<fragment
    android:id="@+id/nav_contactanos"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
</fragment>

第二个导航图中的第二段代码(navigation_initial)

The second fragment of code in second navigation graph (navigation_inicial)

    <fragment
    android:id="@+id/nav_contactanos_cliente"
    android:name="com.engie.mexico.micuenta.Fragments.FragmentContactanosCliente"
    tools:layout="@layout/fragment_contactanos_cliente" >
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_mensaje_usuario"
        app:destination="@id/nav_mensaje_usuario" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_terminos_condiciones"
        app:destination="@id/nav_terminos_condiciones" />
    <action
        android:id="@+id/action_nav_contactanos_cliente_to_nav_aviso_privacidad"
        app:destination="@id/nav_aviso_privacidad" />
</fragment>

当我尝试 MakeProject 时,构建输出显示错误:

When I try to MakeProject, the build output shows me the error :

C:AndroidMiCuentaappsrcmainjavacomengiemexicomicuentaFragmentsFragmentContactanosCliente.java:478: error: cannot find symbol
            FragmentContactanosClienteDirections.ActionNavContactanosToNavMensajeUsuario action = symbol:   class ActionNavContactanosToNavMensajeUsuario

位置:FragmentContactanosClienteDirections 类

location: class FragmentContactanosClienteDirections

但是当我注释第二段代码(navigation_initial)时,问题就消失了.问题是我需要第二段代码,因为我需要调用片段并与它共享捆绑包、其他信息并显示不同的东西......如何做到这一点?

But when I comment the second piece of code (navigation_inicial), the problem disappears. The thing is that I need the second piece of code because I need to recall fragment and share with it bundles, with other information and show different things... How can achieve this?

我也分享了图表的设计:

I also share the design of graphs:

mobile_navigation

mobile_navigation.xml(设计)

navigation_initial

navigation_inicial.xml(设计)

推荐答案

Directions 类的名称是基于 Fragment 的名称,特定类只能有一个实例一次.根据 Safe Args 的这个问题,当您从另一个图表中的一个 - 最后一个获胜.这就是为什么在不同的图中重复使用相同的片段会使第一个图中的 Directions 类无效.

The name of the Directions class is based on the name of the Fragment and there can only be one instance of a particular class at a time. As per this issue with Safe Args, there is no warning when you are overwriting one Directions class from one from another graph - the last one wins. This is why reusing the same fragment in a different graph invalidates the Directions class from the first graph.

当然,如果你的第二个图的片段有不同的动作、不同的参数或任何不同的东西,它也应该有一个不同的片段类——如果你的单个片段类要使用来自另一个图的参数或动作,它们将失败,因为它们不存在于该图中.

Of course, if your second graph's fragment has different actions, different arguments, or anything at all different about it, it should have a different fragment class as well - if your single fragment class were to use arguments or actions from another graph, they would fail as they would not exist in that graph.

正如那个错误中提到的:

As mentioned in that bug:

对于具有 android:name 的目的地,您希望仅在图表中的一个位置包含该 android:name,从需要访问它的所有位置引用该共享目的地(因为目的地可以访问其父图的任何兄弟目的地).

For a destination with an android:name, you'd want to include that android:name in only one place in your graph, referencing that shared destination from all of the places that need access to it (as a destination can access any sibling destinations of their parent graph).

这篇关于当我将相同的片段添加到第二个导航图时,不识别动作类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆