android - 即使在导航中为目标片段设置参数值后,也无法将值从 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) 中的参数显示错误。
解决方案
看起来您忘记在动作中声明参数(片段一)。而且您不会从 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)
所以你应该把这个方法的值放在片段一中。
您可以通过链接找到详细的文档。
推荐阅读
- javascript - 如何设置 vue3-chartjs 固定高度?
- json - 在 Typescipt 中,如何过滤对象数组中遇到的第一个唯一对象?
- nlp - 创建 NER 标签列的属性错误
- python - while vc.is_playing() 循环永远不会退出
- elasticsearch - 如何从kibana向远程服务器发送请求?
- mongodb - MongoDB - mongodump - 回复 listIndexes 时出错无法解析目录条目
- mysql - 数据透视表的问题,使用braintree附加不起作用
- javascript - 测试中未涵盖的回调函数 - 如何模拟?
- c - 如何声明和定义具有多个可变长度数组的结构?
- vue.js - 如何使 li 项目在 Vue 的侧边栏中处于活动状态