gpt4 book ai didi

Android SafeArgs 生成的操作缺少参数

转载 作者:行者123 更新时间:2023-12-03 09:42:06 25 4
gpt4 key购买 nike

我的项目使用 SafeArgs。我已经切换到其他人创建的分支,并且构建项目会生成编译器错误,因为即使在 nav_graph 中,生成的“~Directions”类方法也会返回 ActionOnlyNavDirections(没有传递给目标 fragment 的参数)接受一个论点。

例如,使用以下 nav_graph.xml:

<fragment
android:id="@+id/fragment_a"
android:name="com.myapp.ui.fragment.FragmentA"
android:label="Fragment A"
tools:layout="@layout/fragment_a">

<argument
android:name="userName"
android:defaultValue=" "
app:argType="string" />
<action
android:id="@+id/action_fragment_a_to_fragmentX"
app:destination="@id/fragmentX" />

<action
android:id="@+id/action_fragment_a_to_homeFragment"
app:destination="@id/homeFragment" />

<action
android:id="@+id/action_fragmentA_to_fragmentC"
app:destination="@id/fragmentC"
app:popUpTo="@id/loginFragment" />

</fragment>

<fragment
android:id="@+id/fragment_b"
android:name="com.myapp.ui.fragment.FragmentB"
android:label="Fragment B"
tools:layout="@layout/fragment_b">

<argument
android:name="from"
app:argType="com.myapp.data.local.model.ToFragmentBFrom"/>

<action
android:id="@+id/action_fragmentB_to_homeFragment"
app:destination="@id/homeFragment" />

<action
android:id="@+id/action_fragmentB_to_fragmentC"
app:destination="@id/fragmentC"
app:popUpTo="@id/homeFragment" />

</fragment>

<fragment
android:id="@+id/fragment_c"
android:name="com.myapp.fragment.fragmentC"
android:label="Fragment C"
tools:layout="@layout/fragment_c">

<argument
android:name="userName"
app:argType="string" />
</fragment>

我结束了以下方向类:
class FragmentADirections private constructor() {
private data class ActionFragmentAToFragmentC(val userName: String) : NavDirections {
override fun getActionId(): Int = R.id.action_fragmentA_to_fragmentC

override fun getArguments(): Bundle {
val result = Bundle()
result.putString("userName", this.userName)
return result
}
}

companion object {
fun actionFragmentAToFragmentX(): NavDirections =
ActionOnlyNavDirections(R.id.action_fragmentA_to_fragmentX)

fun actionFragmentAToHomeFragment(): NavDirections =
ActionOnlyNavDirections(R.id.action_fragmentA_to_homeFragment)

fun actionFragmentAToFragmentC(userName: String): NavDirections =
ActionFragmentAToFragmentC(userName)

fun actionGlobalFragmentA(userName: String = " "): NavDirections =
NavGraphDirections.actionGlobalFragmentA(userName)


}
}

和:
class FragmentBDirections private constructor() {
companion object {
fun actionFragmentBToHomeFragment(): NavDirections =
ActionOnlyNavDirections(R.id.action_fragmentA_to_homeFragment)

fun actionFragmentBToFragmentC(): NavDirections =
ActionOnlyNavDirections(R.id.action_fragmentB_to_fragmentC)
}
}

如您所见,FragmentC 采用“userName”参数,而 actionFragmentAToFragmentC 尊重这一点,而 actionFragmentBToFragmentC 则没有。 我尝试过清理、手动删除构建文件夹、使缓存无效并重新启动以及重建 但生成的类仍然看起来总是一样的。为什么 SafeArgs 为一个方向类而不是另一个生成参数?

如何在构建时调试 SafeArgs 插件以了解更多关于导致此问题的原因?

最佳答案

对我来说,这是由于我的源和目标 fragment 在同一个 xml 文件中但在不同的导航元素下造成的。将两个 fragment 移动到同一个导航元素下会生成参数
我从这里改变了(使用嵌套导航元素)

<navigation 
...
android:id="@+id/nav_main">

<fragment
android:id="@+id/fragment_a"
android:name="com.myapp.ui.fragment.FragmentA"
android:label="Fragment A"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragment_a_to_fragment_b"
app:destination="@id/fragment_b" />
</fragment>

<navigation
...
android:id="@+id/nav_other">
<fragment
android:id="@+id/fragment_b"
android:name="com.myapp.ui.fragment.FragmentB"
android:label="Fragment B"
tools:layout="@layout/fragment_b">

<argument
android:name="from"
app:argType="com.myapp.data.local.model.ToFragmentBFrom"/>
</fragment>
</navigation>
</navigation>
对此
<navigation 
...
android:id="@+id/nav_main">

<fragment
android:id="@+id/fragment_a"
android:name="com.myapp.ui.fragment.FragmentA"
android:label="Fragment A"
tools:layout="@layout/fragment_a">
<action
android:id="@+id/action_fragment_a_to_fragment_b"
app:destination="@id/fragment_b" />
</fragment>

<fragment
android:id="@+id/fragment_b"
android:name="com.myapp.ui.fragment.FragmentB"
android:label="Fragment B"
tools:layout="@layout/fragment_b">

<argument
android:name="from"
app:argType="com.myapp.data.local.model.ToFragmentBFrom"/>
</fragment>
</navigation>
在导航元素或整个图表中导航时,您可能需要双重定义参数(在目标和参数中),尽管我在 documentation 中没有看到任何具体提及。

关于Android SafeArgs 生成的操作缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59536999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com