'FragmentFirstBinding Error with First Fragment XML Changes

I'm in Bumblebee v.2021.1.1. I don't have any database elements in my project. I do have defaults in the first fragment:

import com.my.app.databinding.FragmentFirstBinding

in:

class FirstFragment : Fragment() {

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

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

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

When I make changes to fragment_first.xml the FirstFragment.kt file blows up: In Android Studio the problems tab reads Unresolved reference: FragmentFirstBinding and Variable expected, each several times for each occurrence. I don't understand the Logcat much at all. The only way I've found to correct it is to remove the null safety (?) from:

    private var _binding: FragmentFirstBinding? = null

and then to just add it (the question mark) back in.

Also, I've seen about making databinding true in the build.gradle file, but it was not the default when I chose the Basic Activity template when starting the project. Is it stable to add databinding = true to the build.gradle file?



Solution 1:[1]

Thank goodness! I believe the IDE and Kotlin code both work beautifully and I'm right. I have found the error is generated only if a fragment file and a related XML file are running at the same time.

For example: if I open FirstFragment.kt and leave it open and then open and edit fragment_first.xml then the .kt (Kotlin) file gets the error.

But I don't get the error if I don't have the fragment file open, and then change the XML and then open the fragment file for editing.

In other words, when I make changes to a related XML file and then open the related Kotlin file, it can analyze and adjust to the changes made to the XML file that it has to bind to.

In addition, in an attempt to be clear, it looks like having only one tab (one file) open in Android Studio may prevent this from happening.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1