缺少必需的参数 "name",并且没有 android:defaultValue
Required argument "name" is missing and does not have an android:defaultValue
我的代码有问题我试图在片段之间传递参数但它给出了一个异常
java.lang.IllegalArgumentException: Required argument "name" is missing and does not have an android:defaultValue
当 运行 它。我还是个初学者,所以我真的不知道问题出在哪里。
class ListNotesFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentListNotesBinding>(inflater, R.layout.fragment_list_notes, container, false)
val application = requireNotNull(this.activity).application
val args = ListNotesFragmentArgs.fromBundle(arguments!!).name
val dataSource = NoteDatabase.getInstance(application).noteDatabaseDao
val viewModelFactory = ListNoteViewModelFactory(args, dataSource)
val listNoteViewModel = ViewModelProviders.of(this, viewModelFactory).get(ListNoteViewModel::class.java)
binding.listNoteViewModel = listNoteViewModel
binding.lifecycleOwner = this
binding.addButton.setOnClickListener{
this.findNavController().navigate(R.id.action_listNotesFragment_to_detailNoteFragment)
}
listNoteViewModel.navigateToDetailNoteFragment.observe(viewLifecycleOwner, Observer{
it?.let {
this.findNavController().navigate(ListNotesFragmentDirections
.actionListNotesFragmentToDetailNoteFragment(it.noteId))
listNoteViewModel.doneNavigating()
}
})
return binding.root
}
}
所以你的问题是这一行:
val args = ListNotesFragmentArgs.fromBundle(arguments!!).name
在 null 时保护自己的方法是在 nav_graph.xml 中设置一个 defaultValue参数所以它看起来像这样:
<argument
android:name="name"
app:argType="string"
app:nullable="true"
android:defaultValue="@null"/>
我的代码有问题我试图在片段之间传递参数但它给出了一个异常
java.lang.IllegalArgumentException: Required argument "name" is missing and does not have an android:defaultValue
当 运行 它。我还是个初学者,所以我真的不知道问题出在哪里。
class ListNotesFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = DataBindingUtil.inflate<FragmentListNotesBinding>(inflater, R.layout.fragment_list_notes, container, false)
val application = requireNotNull(this.activity).application
val args = ListNotesFragmentArgs.fromBundle(arguments!!).name
val dataSource = NoteDatabase.getInstance(application).noteDatabaseDao
val viewModelFactory = ListNoteViewModelFactory(args, dataSource)
val listNoteViewModel = ViewModelProviders.of(this, viewModelFactory).get(ListNoteViewModel::class.java)
binding.listNoteViewModel = listNoteViewModel
binding.lifecycleOwner = this
binding.addButton.setOnClickListener{
this.findNavController().navigate(R.id.action_listNotesFragment_to_detailNoteFragment)
}
listNoteViewModel.navigateToDetailNoteFragment.observe(viewLifecycleOwner, Observer{
it?.let {
this.findNavController().navigate(ListNotesFragmentDirections
.actionListNotesFragmentToDetailNoteFragment(it.noteId))
listNoteViewModel.doneNavigating()
}
})
return binding.root
}
}
所以你的问题是这一行:
val args = ListNotesFragmentArgs.fromBundle(arguments!!).name
在 null 时保护自己的方法是在 nav_graph.xml 中设置一个 defaultValue参数所以它看起来像这样:
<argument
android:name="name"
app:argType="string"
app:nullable="true"
android:defaultValue="@null"/>