使用 NavigationComponents 移动到新片段时出错
Error moving to a new fragment with NavigationComponents
所以,这很奇怪,我有两种方法可以从片段 A 转到片段 B
第一个来自 fab 按钮点击
private fun navigateToCreateTopics(){
fab_goto_topics.setOnClickListener {
findNavController().navigate(R.id.action_destination_my_topics_to_destination_create_topics)
}
}
我有一些项目有一个编辑按钮,该按钮也转到相同的片段,但有一个包来编辑这些值
val bundle = Bundle()
bundle.putBoolean("shouldEdit",true)
bundle.putParcelable("topic",topicAdapter.topicList[position])
findNavController().navigate(R.id.action_destination_my_topics_to_destination_create_topic,bundle)
现在,如果我按下 fab 按钮在没有额外内容的情况下导航到 createTopics 片段,它将抛出此异常
Fragment CreateTopicsFragment{216b801} (69e0b0ac-1352-4daf-aa36-717c2f320cc9) id=0x7f0800ac} does not have any arguments.
而且我不知道为什么,因为我没有向晶圆厂传递任何参数,而且行
requireArguments().let {
...
}
不应执行,因为它检查空值并且没有参数传递
为什么会这样?
根据 requireArguments()
documentation:
Throws IllegalStateException
if no arguments were supplied to the Fragment.
如果你想要一个可为 null 的 Bundle
:
,你会想使用 arguments
arguments?.let {
...
}
当然,您也可以按照define destination arguments documentation,在目的地的<argument>
中添加一个defaultValue
:
<argument
android:name="shouldEdit"
app:argType="boolean"
android:defaultValue="false" />
这将确保您始终设置参数 Bundle 和 false
、默认值或您自己的值。
这种方法在 using Safe Args 时效果特别好,它将用类型安全调用替换整个 let
:
val args: CreateTopicsFragmentArgs by navArgs
val shouldEdit = args.shouldEdit
尝试 arguments?.let{}
requireArguments 内部代码是 public final Bundle requireArguments() { Bundle arguments = getArguments(); if (arguments == null) { throw new IllegalStateException("Fragment " + this + " does not have any arguments."); } return arguments; }
,它抛出 IllegalStateException
所以,这很奇怪,我有两种方法可以从片段 A 转到片段 B
第一个来自 fab 按钮点击
private fun navigateToCreateTopics(){
fab_goto_topics.setOnClickListener {
findNavController().navigate(R.id.action_destination_my_topics_to_destination_create_topics)
}
}
我有一些项目有一个编辑按钮,该按钮也转到相同的片段,但有一个包来编辑这些值
val bundle = Bundle()
bundle.putBoolean("shouldEdit",true)
bundle.putParcelable("topic",topicAdapter.topicList[position])
findNavController().navigate(R.id.action_destination_my_topics_to_destination_create_topic,bundle)
现在,如果我按下 fab 按钮在没有额外内容的情况下导航到 createTopics 片段,它将抛出此异常
Fragment CreateTopicsFragment{216b801} (69e0b0ac-1352-4daf-aa36-717c2f320cc9) id=0x7f0800ac} does not have any arguments.
而且我不知道为什么,因为我没有向晶圆厂传递任何参数,而且行
requireArguments().let {
...
}
不应执行,因为它检查空值并且没有参数传递
为什么会这样?
根据 requireArguments()
documentation:
Throws
IllegalStateException
if no arguments were supplied to the Fragment.
如果你想要一个可为 null 的 Bundle
:
arguments
arguments?.let {
...
}
当然,您也可以按照define destination arguments documentation,在目的地的<argument>
中添加一个defaultValue
:
<argument
android:name="shouldEdit"
app:argType="boolean"
android:defaultValue="false" />
这将确保您始终设置参数 Bundle 和 false
、默认值或您自己的值。
这种方法在 using Safe Args 时效果特别好,它将用类型安全调用替换整个 let
:
val args: CreateTopicsFragmentArgs by navArgs
val shouldEdit = args.shouldEdit
尝试 arguments?.let{}
requireArguments 内部代码是 public final Bundle requireArguments() { Bundle arguments = getArguments(); if (arguments == null) { throw new IllegalStateException("Fragment " + this + " does not have any arguments."); } return arguments; }
,它抛出 IllegalStateException