Wht when I use view binding for fragment, my app crass without any reason?

In your adapter getView() you are returning binding.root which refers to the fragment layout binding. You should be returning menuRow.root instead.

And you should have an exception in the logcat. Just make sure you don't have any logcat filters or such that hide it.


Your onCreateView(...) should only do

 override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {

        _binding = MyFragment.inflate(inflater, container, false)
        return binding.root
}

All the other stuff you're doing should be in:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    
    val listView: ListView = binding.listViewMenu
    val adapter = MenuAdapter(this, binding.title, images)
    listView.adapter = adapter
    ... etc

}

I would suggest a few changes to your code. The Fragment class can now receive a xml layout so you should pass it there. The change would look like this:

class MyFragment : Fragment(R.layout.my_main_fragment) // replace with the name of your layout

Each XML layout gets a respective binding class that corresponds with the XML layout name. For example, if your layout is called my_main_fragment, the class will receive the name of MyMainFragmentBinding. Following that change, you will bind your view inside the onViewCreated() method. It will look like:

    private var _binding: MyMainFragmentBinding? = null
    private  val binding get() = _binding!!

    override fun onViewCreated(
        view: View,
        savedInstanceState: Bundle?) 
    {
        super.onViewCreated(view, savedInstanceState)
        _binding = MyMainFragmentBinding.bind(view)
    
    // ...other code from onCreateView
    }

    internal inner class MenuAdapter(context: MyFragment, ...

Also, don't forget to add _binding = null in the onDestroyView() method

To answer the question from the comments: Inside getView() of your MenuAdapter, you should be returning menuRow.root.

You can read more about view binding at the android developers page: https://developer.android.com/topic/libraries/view-binding

Or see an example here: https://github.com/android/architecture-components-samples/blob/master/ViewBindingSample/app/src/main/java/com/android/example/viewbindingsample/BindFragment.kt#L36-L41