首页 > 解决方案 > 即使在导航中为目标片段设置参数值后,也无法将值从 FragmentOne 传递到 FragmentTwo

问题描述

FragmentTwo 片段类代码:

class FragmentTwo : Fragment() {


    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?,
    ): View? {
        // Inflate the layout for this fragment
        val binding : FragmentTwoBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_two, container, false)
        var args = FragmentTwoArgs.fromBundle(arguments)
        setHasOptionsMenu(true)
        return binding.root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater?.inflate(R.menu.overflow_menu,menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return NavigationUI.onNavDestinationSelected(item!!,findNavController())
                || super.onOptionsItemSelected(item)
    }
}

FragmentOne 片段类代码:

class FragmentOne : Fragment() {
    var nameValue = "Abhas"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_one, container, false)
        val binding: FragmentOneBinding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_one, container, false)
        binding.clickable = this

        binding.button.setOnClickListener {
            findNavController().navigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())
        }
        return binding.root
    }
}

导航xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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/navigation"
    app:startDestination="@id/fragmentOne">

    <fragment
        android:id="@+id/fragmentOne"
        android:name="com.example.fragmentpractise1.FragmentOne"
        android:label="fragment_one"
        tools:layout="@layout/fragment_one" >
        <action
            android:id="@+id/action_fragmentOne_to_fragmentTwo"
            app:destination="@id/fragmentTwo" />
    </fragment>
    <fragment
        android:id="@+id/fragmentTwo"
        android:name="com.example.fragmentpractise1.FragmentTwo"
        android:label="fragment_two"
        tools:layout="@layout/fragment_two" >
        <argument
            android:name="nameValue"
            app:argType="string" />
    </fragment>
    <fragment
        android:id="@+id/aboutFragment"
        android:name="com.example.fragmentpractise1.AboutFragment"
        android:label="fragment_about"
        tools:layout="@layout/fragment_about" />
</navigation>

现在,当我在 FragmentTwo 类中设置 args 变量时,参数在 fromBundle(arguments) 中显示错误。我尝试在导航时在 FragmentOne 的 setOnclicklistener 中给出参数,但它没有在构造函数中要求任何类型的值。我无法理解为什么 FragmentTwo 类的 fromBundle(arguments) 中的参数显示错误。

标签: androidkotlin

解决方案


看起来您忘记在动作中声明参数(片段一)。而且您不会从 fragment_one 发送任何内容。

您应该将参数添加到导航 xml 中的 fragment_one 内的操作:

<action
    android:id="@+id/action_fragmentOne_to_fragmentTwo"
                app:destination="@id/fragmentTwo">
        <argument
            android:name="nameValue"
            app:argType="string"
            android:defaultValue="default" />
    </action>

然后重新构建应用程序 - 将使用字符串参数生成另一个导航操作方法。

FragmentOneDirections.actionFragmentOneToFragmentTwo(nameValue : String)

所以你应该把这个方法的值放在片段一中。

您可以通过链接找到详细的文档。


推荐阅读