Home > Software design >  DataBindingUtil type mismatch
DataBindingUtil type mismatch

Time:11-11

I'm copying the code here; https://developer.android.com/codelabs/kotlin-android-training-view-model#5

But I'm getting a type mismatch from the DataBindingUtil.inflate method. It's returning ViewDataBinding!, when FragmentPlayBinding is expected.

I've downloaded the working app here https://github.com/google-developer-training/android-kotlin-fundamentals-apps, and checked all dependencies etc. Everything looks good. Although that is an older project.

My code;

class PlayFragment : Fragment() {
private val TAG = "PlayFragment"
private lateinit var binding: FragmentPlayBinding
private lateinit var viewModel: PlayViewModel

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

    //Inflate view and obtain an instance of the binding class
    binding = DataBindingUtil.inflate(
        inflater,
        R.layout.fragment_play,
        container,
        false
    )
    Log.i(TAG, "Called ViewModelProvider.get")
    viewModel = ViewModelProvider(this).get(PlayViewModel::class.java)

    //viewBinding.correctButton.setOnClickListener { onCorrect() }
    //viewBinding.skipButton.setOnClickListener { onSkip() }
    //updateScoreText()
    //updateWordText()
    return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    Log.d(TAG, "onViewCreated")
}

}

Some things I've tried;

  1. checked all dependencies. I've got the same as the working example project, but with the latest versions
  2. cleaned and rebuilt project
  3. invalidated and rebuilt caches
  4. made sure all namespaces are all lowercase

I am using Android Studio Bumblebee | 2021.1.1 Beta 3 (as I said, this code in example project works).

thanks

CodePudding user response:

Change this Line from : binding = DataBindingUtil.inflate( inflater, R.layout.fragment_play, container, false )

to : binding = FragmentPlayBinding.inflate(inflater, container, false)

CodePudding user response:

var binding = FragmentPlayBinding.inflate(inflater, container, false);
//set variables in Binding
return binding.getRoot();
  • Related