【问题标题】:Too many arguments for public final fun actionConformationFragmentToHomeFragment()public final fun actionConformationFragmentToHomeFragment() 的参数太多
【发布时间】:2020-06-16 06:54:39
【问题描述】:

我想使用 NavDirections 将我的参数发送到另一个片段

<fragment
    android:id="@+id/conformationFragment"
    android:name="com.example.panoramic.ui.ConformationFragment"
    android:label="ConformationFragment"
    tools:layout="@layout/fragment_conformation">
        <action
            android:id="@+id/action_conformationFragment_to_registerProductFragment"
            app:destination="@id/registerProductFragment" />
        <action
            android:id="@+id/action_conformationFragment_to_homeFragment"
            app:destination="@id/homeFragment" />
        <argument
            android:name="modelNumber"
            app:argType="string" />
        <argument
            android:name="serialNumber"
            app:argType="string" />
</fragment>

这是我尝试发送参数的方式:

class ConformationFragment : Fragment(R.layout.fragment_conformation) {

    private var fragmentConformationBinding: FragmentConformationBinding? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val binding = FragmentConformationBinding.bind(view)
        fragmentConformationBinding = binding
        val modelNumber = binding.modelValue.text.toString()
        val serialNumber = binding.serialValue.text.toString()
        binding.editingInformationButton.setOnClickListener {
            findNavController().navigate(R.id.action_conformationFragment_to_registerProductFragment)
        }
        binding.confirmButton.setOnClickListener {
            findNavController().navigate(ConformationFragmentDirections
                .actionConformationFragmentToHomeFragment(modelNumber, serialNumber))
        }

    }

    override fun onDestroyView() {
        fragmentConformationBinding = null
        super.onDestroyView()
    }
}

我确定依赖项是正确的,但仍然出现错误:

Too many arguments for public final fun actionConformationFragmentToHomeFragment(): NavDirections defined in com.example.panoramic.ui.ConformationFragmentDirections.Companion
and red line under modelNumber and serialNumber in actionConformationFragmentToHomeFragment() function

我检查了其他类似的问题,但没有一个对我有用。

【问题讨论】:

    标签: android android-studio android-fragments kotlin


    【解决方案1】:

    问题是您在导航图文件中定义的参数被ConformationFragment 接受,而不是HomeFragment

    参数应该在接受已定义参数的片段的&lt;fragment&gt; 标记下定义,因此对该片段的所有其他导航操作都将接受参数。这就是关于ConformatioFragmentHomeFragment 的导航图的声明方式:

    <fragment
        android:id="@+id/conformationFragment"
        android:name="com.example.panoramic.ui.ConformationFragment"
        android:label="ConformationFragment"
        tools:layout="@layout/fragment_conformation">
            <action
                android:id="@+id/action_conformationFragment_to_registerProductFragment"
                app:destination="@id/registerProductFragment" />
            <action
                android:id="@+id/action_conformationFragment_to_homeFragment"
                app:destination="@id/homeFragment" />
    </fragment>
    
    <fragment 
        android:id="@+id/homeFragment"
        android:name="com.example.panoramic.ui.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/fragment_home">
            <argument
                android:name="modelNumber"
                app:argType="string" />
            <argument
                android:name="serialNumber"
                app:argType="string" />
    </fragment>
    

    进行这些更改后,您应该能够将参数传递给actionConformationFragmentToHomeFragment() 函数。

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多