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

1

13

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
    }

}
Cubbyhole answered 16/4, 2020 at 10:7 Comment(0)
S
19

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"/>
Samella answered 17/4, 2020 at 23:40 Comment(3)
For me if I look into crashlytics whi this is coming for only few users and not all?Fourpence
Also for me into crashlytics this is coming for one user twice didn't get the exact reason behind this,Merman
Can you please link your post as what you are refering to might be another situation...Samella

© 2022 - 2024 — McMap. All rights reserved.