Reputation: 3
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
}
}
inflate is showing this error and
binding.root is showing this error
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'
}
Upvotes: 0
Views: 239
Reputation: 19602
You're not using the generated binding class, you're just creating a new BirthdayGreetingActivity
instance and then trying to call inflate
on it (which isn't a method on an Activity)
// wrong (and you should never be constructing Activity objects yourself!)
binding = BirthdayGreetingActivity().inflate(layoutInflater)
Assuming your layout file is R.layout.birthday_greeting_activity
, then view binding will generate a binding class called BirthdayGreetingActivityBinding
. That class has a static method inflate
, which will produce a BirthdayGreetingActivityBinding
object, and that's what you should be storing.
lateinit var binding: BirthdayGreetingActivityBinding
...
binding = BirthdayGreetingActivityBinding.inflate(layoutInflater)
And if you look at your working MainActivity
one, you'll see that's exactly how it's being handled there, with the equivalent binding class
Upvotes: 0
Reputation: 16
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
Upvotes: 0