Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required argument "name" is missing and does not have an android:defaultValue

I'm having a problem with my code i'm trying to pass arguments between fragments but it gives an exception java.lang.IllegalArgumentException: Required argument "name" is missing and does not have an android:defaultValue when run it. Im still still a beginner so i can't really tell where the problem is coming from.

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
    }

}
like image 709
Lavdim haliti Avatar asked Apr 16 '20 10:04

Lavdim haliti


People also ask

How do I declare a default value of null in Android?

If an argument type supports null values, you can declare a default value of null by using android:defaultValue="@null". Routes, deep links, and URIs with their arguments can be parsed from strings.

How do I override the default value of an argument?

If needed, you can override the default value of an argument (or set one if it doesn't already exist) by defining an argument at the action level. This argument must be of the same name and type as the argument declared in the destination.

How to set the value of arguments in a confirmationaction?

For example, if the action is called confirmationAction, the class is named ConfirmationAction. If your action contains arguments without a defaultValue, then you use the associated action class to set the value of the arguments.

How do you set the value of an action argument?

For example, if the action is called confirmationAction, the class is named ConfirmationAction. If your action contains arguments without a defaultValue, then you use the associated action class to set the value of the arguments. A class is created for the receiving destination.


1 Answers

So your issue is this row:

val args = ListNotesFragmentArgs.fromBundle(arguments!!).name

The way to guard yourself from when it's null is to set a defaultValue in your nav_graph.xml argument so it looks something like this:

<argument
    android:name="name"
    app:argType="string"
    app:nullable="true"
    android:defaultValue="@null"/>
like image 123
Lucho Avatar answered Oct 06 '22 21:10

Lucho