当我将相同的片段添加到第二个导航图时,无法识别动作类 [英] When I add the same fragment to second navigation graph, don´t recognize the Action Class
问题描述
我需要将相同的片段添加到不同的导航图.这是第一张图的代码( 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_inicial )
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:\Android\MiCuenta\app\src\main\java\com\engie\mexico\micuenta\Fragments\FragmentContactanosCliente.java:478: error: cannot find symbol
FragmentContactanosClienteDirections.ActionNavContactanosToNavMensajeUsuario action = symbol: class ActionNavContactanosToNavMensajeUsuario
位置:FragmentContactanosClienteDirections类
location: class FragmentContactanosClienteDirections
但是当我注释第二段代码( navigation_inicial )时,问题消失了.关键是我需要第二段代码,因为我需要调用片段并与它捆绑在一起,与其他信息一起共享,并展示不同的东西...如何做到这一点?
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
navigation_inicial
推荐答案
Directions
类的名称基于Fragment的名称,并且在该位置只能有一个特定类的实例.一个时间.根据安全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.
如该错误中所述:
对于具有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屋!