1

When I try to import databinding to MainActivity, I get an "Unresolved reference: databinding" error.

MainActivity file:

package <package-name>

import <package-name>.databinding.FragmentLoginBinding

class LoginFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val binding = DataBindingUtil.inflate<FragmentLoginBinding>(inflater,
            R.layout.fragment_login,container,false)

        binding.register_btn.setOnClickListener { view : View ->
            view.findNavController().navigate(R.id.action_loginFragment_to_welcomeFragment)
        }

        binding.login_btn.setOnClickListener { view : View ->
            view.findNavController().navigate(R.id.action_loginFragment_to_welcomeFragment)
        }

        return binding.root
    }
}

where package-name is my package name. The package name is recognized, only databinding is not, so it shows in red.

I guess because of that, FragmentLoginBinding in

val binding = DataBindingUtil.inflate<FragmentLoginBinding>(inflater,
            R.layout.fragment_login,container,false)

is not recognized too.

Project build.gradle file:

buildscript {
    ext{
        kotlin_version = '1.3.61'
        gradle_version = '3.6.0'
        data_binding_compiler_version = gradle_version
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
    }
}

App build.gradle file:

apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled = true
    }
}

dependencies {
    kapt "androidx.databinding:databinding-compiler:$data_binding_compiler_version"
}

I already tried to follow this codelab https://codelabs.developers.google.com/codelabs/kotlin-android-training-add-navigation/#0 and also search for solutions, but wasn't able to fix it.

Anyone can help, please?

Thanks in advance.

Nelson Lopes
  • 117
  • 2
  • 13
  • Most likely it is because of an error in the data binding compiler. In other words there must be an error in some layout file for which the Databinding library is unable to generate the binding classes. You should check the gradle build logs for databinding compiler errors which are often at the end of the build log. – Som Feb 28 '20 at 14:06
  • In my case the solution was: https://stackoverflow.com/a/66237310/3613808 – Panayot Feb 17 '21 at 07:20

2 Answers2

1

Looks like I was missing the layout tag in the layout to support Databinding:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
Nelson Lopes
  • 117
  • 2
  • 13
0

correct Answer

Also wrap your root view with tag

  var cropAdviceBinding: TemplateCropAdviceBinding? = null
        cropAdviceViewStub.viewStub?.apply {
            setOnInflateListener { _, inflated ->
                cropAdviceBinding =
                    DataBindingUtil.bind(inflated)
            }
            inflate()
        }
Vikas Pandey
  • 1,115
  • 1
  • 15
  • 25