'Fragment has null arguments
i have a problem. when i try to use safeargs to setText in my dialog EditFragment and then click my itemview it keep crash and show 'java.lang.IllegalStateException: Fragment EditFragment{da9aa86} (406b74da-50ae-4b39-a9fb-7d393ad9fb4e) has null arguments' in log. But when i try to change using findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToEditFragment(type)) no error and it go to my EditFragment instead of showing like Dialog, i want to make it show Dialog.
this is my HomeAdapter
class HomeAdapter(val listener: OnItemClickListener): RecyclerView.Adapter<HomeAdapter.ViewHolder>() {
var list = emptyList<NotesEntity>()
var onLoad: (() -> Unit)? = null
inner class ViewHolder(val binding: ListNotesBinding): RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
...
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val type = list[position]
holder.binding.tvTitle.text = type.title
holder.binding.tvIsiNotes.text = type.content
holder.itemView.setOnClickListener{
listener.onItemClick(type)
}
}
interface OnItemClickListener {
fun onItemClick(type: NotesEntity)
}
this is my EditFragment
class EditFragment : DialogFragment() {
lateinit var binding: FragmentEditBinding
private val args by navArgs<EditFragmentArgs>()
private lateinit var notesViewModel: NotesViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentEditBinding.inflate(inflater, container, false)
notesViewModel = ViewModelProvider(this).get(NotesViewModel::class.java)
binding.tvTitleEdit.setText(args.currentNotes.title)
binding.tvContentEdit.setText(args.currentNotes.content)
binding.btnEditNotes.setOnClickListener {
editNotes()
}
return binding.root
}
private fun editNotes(){
...
}
}
and this is my HomeFragment (the error one)
val adapter = HomeAdapter(object: HomeAdapter.OnItemClickListener{
override fun onItemClick(type: NotesEntity) {
val dialogFragment = EditFragment()
dialogFragment.show(requireActivity().supportFragmentManager, null)
}
})
binding!!.rvNotes.adapter = adapter
this is my HomeFragment (no error but it go to my EditFragment instead of showing like Dialog)
val adapter = HomeAdapter(object: HomeAdapter.OnItemClickListener{
override fun onItemClick(type: NotesEntity) {
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToEditFragment(type))
})
binding!!.rvNotes.adapter = adapter
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
