使用导航组件将一个片段的值传递给另一个片段时未生成 FragmentArgs class
FragmentArgs class in not generated while passing value of one fragment to another by using Navigation components
这是我的 FragmentTwo
片段 class 代码。
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 代码:
class FragmentOne : Fragment() {
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
return binding.root
}
fun onClicking() {
//Toast.makeText(activity, "You clicked me.", Toast.LENGTH_SHORT).show()
//findNavController().navigate(R.id.action_fragmentOne_to_fragmentTwo)
findNavController().navigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())
}
}
这是我的导航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" />
<argument
android:name="numViews"
app:argType="integer"
android:defaultValue="18" />
</fragment>
<fragment
android:id="@+id/fragmentTwo"
android:name="com.example.fragmentpractise1.FragmentTwo"
android:label="fragment_two"
tools:layout="@layout/fragment_two" />
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.fragmentpractise1.AboutFragment"
android:label="fragment_about"
tools:layout="@layout/fragment_about" />
</navigation>
现在我在 FragmentTwo
class 代码中遇到错误,因为在将其分配给 args 变量时未生成 FragmentTwoArgs
class。我正在使用 Nav safe args 并通过 Nav graph 在 FragmentOne
中定义参数值。
任何帮助将不胜感激。
您使用了错误的 Fragment
来声明参数。如果您希望 FragmentTwo
有参数,您应该在导航中使用片段 xml:
<fragment
android:id="@+id/fragmentTwo"
android:name="com.example.fragmentpractise1.FragmentTwo"
android:label="fragment_two"
tools:layout="@layout/fragment_two">
<argument
android:name="numViews"
app:argType="integer"
android:defaultValue="18" />
</fragment>
您可能还想使用惰性委托 navArgs()
:
private val args: FragmentTwoArgs by navArgs()
这是我的 FragmentTwo
片段 class 代码。
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 代码:
class FragmentOne : Fragment() {
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
return binding.root
}
fun onClicking() {
//Toast.makeText(activity, "You clicked me.", Toast.LENGTH_SHORT).show()
//findNavController().navigate(R.id.action_fragmentOne_to_fragmentTwo)
findNavController().navigate(FragmentOneDirections.actionFragmentOneToFragmentTwo())
}
}
这是我的导航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" />
<argument
android:name="numViews"
app:argType="integer"
android:defaultValue="18" />
</fragment>
<fragment
android:id="@+id/fragmentTwo"
android:name="com.example.fragmentpractise1.FragmentTwo"
android:label="fragment_two"
tools:layout="@layout/fragment_two" />
<fragment
android:id="@+id/aboutFragment"
android:name="com.example.fragmentpractise1.AboutFragment"
android:label="fragment_about"
tools:layout="@layout/fragment_about" />
</navigation>
现在我在 FragmentTwo
class 代码中遇到错误,因为在将其分配给 args 变量时未生成 FragmentTwoArgs
class。我正在使用 Nav safe args 并通过 Nav graph 在 FragmentOne
中定义参数值。
任何帮助将不胜感激。
您使用了错误的 Fragment
来声明参数。如果您希望 FragmentTwo
有参数,您应该在导航中使用片段 xml:
<fragment
android:id="@+id/fragmentTwo"
android:name="com.example.fragmentpractise1.FragmentTwo"
android:label="fragment_two"
tools:layout="@layout/fragment_two">
<argument
android:name="numViews"
app:argType="integer"
android:defaultValue="18" />
</fragment>
您可能还想使用惰性委托 navArgs()
:
private val args: FragmentTwoArgs by navArgs()