【问题标题】:Too many arguments for public final fun actionFragmentAToDetailActivity()public final fun actionFragmentAToDetailActivity() 的参数太多
【发布时间】:2021-10-21 13:18:03
【问题描述】:

实际上 DetailActivity(需要传递参数)存在于库模块中,我需要从应用程序(FragmentA)启动它,但我收到如下构建错误:

Too many arguments for public final fun actionFragmentAToDetailActivity()

我正在使用 SafeArgs 库来传递数据。

片段A

 findNavController().navigate(
            FragmentADirections.actionFragmentAToDetailActivity(
                "id",
                "name"
            )
        )

nav_graph_app.xml(存在于应用模块中)

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_a_navigation"
app:startDestination="@id/fragmentA">

<fragment
    android:id="@+id/fragmentA"
    android:name="com.abhishek.fragmentA"
    android:label="WeeklyWorkListFragment"
    tools:layout="@layout/fragment_a">
    
    <action
        android:id="@+id/action_fragmentA_to_DetailActivity"
        app:destination="@id/detail_activity" />
</fragment>
</navigation>

nav_graph_common_component.xml(存在于库项目中,作为库模块包含在 app 的 build.gradle 中)

<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/common_component_navigation"

<activity
    android:id="@+id/detail_activity"
    android:name="com.components.DetailActivity"
    android:label="DetailActivity"
    tools:layout="@layout/activity_detail">
    <argument
        android:name="id"
        app:argType="string" />
    <argument
        android:name="name"
        app:argType="string" />
</activity>
</navigation>

我们将不胜感激任何帮助或指导。

【问题讨论】:

  • 你能在 github 上发布一个简单的项目来重现这个问题吗?

标签: android android-architecture-navigation android-navigation-graph android-safe-args


【解决方案1】:

你只需要添加:

&lt;include app:graph="@navigation/nav_graph_common_component" /&gt;

在您的 nav_graph_app.xml

【讨论】:

    【解决方案2】:

    片段A

    findNavController().navigate(
                    FragmentADirections.actionFragmentAToDetailActivity(
                        "id"
                    ).setname("name")
                )
    

    【讨论】:

      【解决方案3】:

      1.尝试使用带有串行的bundle

      2.

      val bundle = Bundle().apply {
                      putSerializable("article", it)
                  }
                  findNavController().navigate(
                      R.id.action_savedNewsFragment_to_articleNewsFragment,
                      bundle
                  )
      
      1. 在你的片段中

      val args:ArticleNewsFragmentArgs by navArgs()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-19
        • 1970-01-01
        相关资源
        最近更新 更多