Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding type mismatch

So I have no idea why but I get a type mismatch with data binding. I have the exact same code used for data binding in another app and it works fine. Error is: Required ActivityMainBinding, Found:ViewDataBinding!

and if I just delete the assignment to binding, DataBinding.setContentView highlights in red saying: Not enough information to infer type variable T

It always worked, I don't know if it's some update or something but I have no idea where to start trying to fix this.

MainActivity.kt

package com.pdstudios.practice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.pdstudios.practice.databinding.ActivityMainBinding
import androidx.databinding.DataBindingUtil

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main) //PROBLEM HERE
        setContentView(binding.root)
    }
} 

build.gradle(:app)

    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.pdstudios.practice"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildFeatures {
        dataBinding = true
        viewBinding = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
like image 990
ProgrammerPete Avatar asked Oct 24 '25 02:10

ProgrammerPete


1 Answers

Check if you're not missing layout tag as your top root layout in your xml file.

Wrap your xml layout in a <layout></layout> and DataBindingUtil will work as intended.

like image 147
M.Ed Avatar answered Oct 25 '25 17:10

M.Ed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!