androidkotlinbindingandroid-inflate

Binding working fine in MainActivity.kt but not working properly on BirthdayGreetingActivity.kt (another activity file). Unresolved reference: inflate


I was learning kotlin with binding while MainActivity.kt is working completely fine, when I am trying to do the same steps in another Activity named BirthdayGreetingActivity.kt , inflate method is not working and giving error saying Unresolved reference: inflate. Also at binding.root, it is also giving error saying Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: internal val File.root: File defined in kotlin.io

I already added

  buildFeatures {
        viewBinding true
    }

in the build.gradle file.

this is my MainActivity.kt file -

package com.example.birthdaygreet

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.birthdaygreet.databinding.ActivityBirthdayGreetingBinding
import com.example.birthdaygreet.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*setContentView(R.layout.activity_main)*/
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

    }

    fun showMessage(view: View) {

        val name = binding.enterNameField.editableText.toString()

       val intent = Intent (this, BirthdayGreetingActivity::class.java)
        intent.putExtra(BirthdayGreetingActivity.NAME_EXTRA, name)
        startActivity(intent)

    }
}

and this is my second activity file BirthdayGreetingActivity.kt file -

package com.example.birthdaygreet

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle


class BirthdayGreetingActivity : AppCompatActivity() {

    companion object {
        const val NAME_EXTRA = "name_extra"
    }

    private lateinit var binding: BirthdayGreetingActivity


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*setContentView(R.layout.activity_birthday_greeting)*/
        binding = BirthdayGreetingActivity().inflate(layoutInflater)
        setContentView(binding.root)


        val name = intent.getStringExtra(NAME_EXTRA)
        binding.birthdayGreet

    }
}

Red problems screenshot

inflate is showing this error and

inflate error screenshot

binding.root is showing this error

binding.root error screenshot

None of these error is showing in the MainActivity.kt but only showing in BirthdayGreetingActivity.kt

please help me resolve this issue...

I tried copying everything from MainActivity.kt but it did not worked. I already added

    buildFeatures {
        viewBinding true
    }

    dataBinding {
        enabled = true
    }

in Build.gradle file.

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-parcelize'
}

android {
    namespace 'com.example.birthdaygreet'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.birthdaygreet"
        minSdk 28
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildFeatures {
        viewBinding true
    }

    dataBinding {
        enabled = 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 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Solution

  • You have made the object binding as object of type BirthdayGreetingActivity. It should be of type layoutbinding. that is for example:

    private lateinit var binding: BirthdayGreetingActivityBinding
    

    or whatever layout you want to inflate followed by 'Binding' word.

    Similarly, for

    binding = BirthdayGreetingActivity().inflate(layoutInflater).
    

    or you can use,

    override val bindingInflater: (LayoutInflater) -> BirthdayGreetingActivityBinding =
            BirthdayGreetingActivityBinding::inflate