'viewBinding is not working with error (Android studio 4)
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)
}
}
Solution 1:[1]
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.
Solution 2:[2]
One of these might help
- Rebuild project (Build->Rebuild Project)
- File -> Invalidate Chaches -> Invalidate and Restart
Solution 3:[3]
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.
Solution 4:[4]
Just replace:
setContentView(R.layout.activity_main)
with:
setContentView(binding.root)
Passing your layout separately will inflate the second layout without any view bindings.
Solution 5:[5]
More easy solve. Broke the xml. Set space in id or not close tag. ViewBinding recreate broken xml and return xml to correct state.
Solution 6:[6]
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.
Solution 7:[7]
change the setContentView(R.layout.activity_main) with setContentView(view) or setContentView(binding.root)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Farees Hussain |
Solution 3 | |
Solution 4 | Ugre |
Solution 5 | Igor Arny |
Solution 6 | Makrov H |
Solution 7 | nesnes |