Reputation: 61
I make android application with kotlin by Android studio 4 (4.11).
findViewById is deprecated in Androd Studio 4 ,then I use viewBinding. https://developer.android.com/topic/libraries/view-binding
But viewBinding is not working with error.
(path)/MainActivity.kt: (6, 31): Unresolved reference: ActivityMainBinding
Can someone tell me the reason for the error or my mistake?
code is below.
build.grade:
android {
...
// <-- added
buildFeatures {
viewBinding = true
}
// -->
}
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/viewText" // added.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
private lateinit var binding: ActivityMainBinding // added. <== error (6:31)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// <-- added.
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
binding.textView.text = "Test view binding."
// -->
setContentView(R.layout.activity_main)
}
}
Upvotes: 6
Views: 23350
Reputation: 1
1.Add this in build.gradle
(module: app):
In android braket:
buildFeatures {
viewBinding = true
}
2.In your Java file create binding variable
ActivityMainBinding binding;
OnCrete()
do like this:binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Hope this work :)
Upvotes: -1
Reputation: 31
More easy solve. Broke the xml. Set space in id or not close tag. ViewBinding recreate broken xml and return xml to correct state.
Upvotes: 1
Reputation: 11
Just replace:
setContentView(R.layout.activity_main)
with:
setContentView(binding.root)
Passing your layout separately will inflate the second layout without any view bindings.
Upvotes: 1
Reputation: 127
I was also receiving a data binding error when building my project with view bindings enabled. This question and some of the answers may be useful for anyone having this issue with Arctic Fox + Java 11. After upgrading, I changed the compile options in the app build.gradle entry to:
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
//Required if using new Java 11 language features in your project
I was receiving an error: package not found (*.databinding) and thought that view bindings were not being generated. After trying numerous remedies and finding the generated bindings, I came across the above question. Since I am not using any of the new language features in my project, I changed the compile options in the build.gradle back to:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Rebuilt the project and the bindings were all back with no compile errors.
As per teralser's answer, "this issue has been fixed and landed in BB canary 8. It is specific to Java 11".
For devices running Android 4.0 (API level 14) or higher with Android Gradle Plugin 3.0.0+, adding the Java 8 compile options as per link may solve your issue.
Upvotes: 13
Reputation: 181
change the setContentView(R.layout.activity_main) with setContentView(view) or setContentView(binding.root)
Upvotes: -1
Reputation: 477
Very easy to solve. Create a copy of the layout you are applying on the window and use this copied layout from now on, solved.
Upvotes: -1
Reputation: 121
Do this:- binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view) or setcontentview(binding.root)
binding.textView.text = "Test view binding."
it should work.
Upvotes: 4
Reputation: 181
One of these might help
Upvotes: 9