how i can fix databinding error while inflate view - android

When I normally I app run after building android studio throws this error.
at com.shayan.test.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:113)
E/AndroidRuntime: at com.shayan.test.MainActivity.onCreate(MainActivity.kt:14)
I think I forget something to do calling method or something
package com.shayan.test
package com.shayan.test
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.shayan.test.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
(binding.root)
}
}

your using viewbinding.. setContentView is missing. and
binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
change to
binding = ActivityMainBinding.inflate(layoutInflater)
Full code for example
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

Try this methods :
//Try to add in build.gradle (Module:app)
buildFeatures {
viewBinding true
}
2:
Build -> Clean Project
Build -> Rebuild Project
If this method does not help you then It's not about the Gradle file. It is about the XML file.

Related

change text in android studio using viewBinding

I don't know why text is not changing in the BirthdayGreetingActivity.
No error is coming but it's not changing text. I checked(by Logcat) that name has came successfully to BirthdayGreetingActivity.kt from MainActivity.kt.
I guess that this [binding.birthdayGreeting.text = "Happy Birthday $name"] line in BirthdayGreetingActivity.kt is not working properly.
MainActivity.kt --
package com.example.firstapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.firstapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
fun createBirthdayCard(view: View){
val name = binding.nameInput.editableText.toString()
// Toast.makeText(this,"Name is $name",Toast.LENGTH_SHORT).show()
val intent = Intent(this,BirthdayGreetingActivity::class.java)
intent.putExtra(BirthdayGreetingActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
BirthdayGreetingActivity.kt --
package com.example.firstapp
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.firstapp.databinding.ActivityBirthdayGreetingBinding
class BirthdayGreetingActivity : AppCompatActivity() {
private lateinit var binding: ActivityBirthdayGreetingBinding
companion object{
const val NAME_EXTRA ="name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBirthdayGreetingBinding.inflate(layoutInflater)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
// val greet = binding.birthdayGreeting.editableText.toString()
// print(name)
// if (name != null) {
// Log.d("greet = ",binding.birthdayGreeting.editableText.toString())
// }
binding.birthdayGreeting.text = "Happy Birthday $name" [maybe something wrong here]
}
}
:) :) Thanks for spending your valuable time to help me :) :)
On BirthdayGreetingActivity you are passing the layout ID for setContentView, instead you must pass the binding.root, just like you did on MainActivity...
Change this line:
setContentView(R.layout.activity_birthday_greeting)
To:
setContentView(binding.root)

What is the cause and sol to the error showing due to view Binding?

On Clicking the btnBmi button, the app is crashing, & in the logcat the error showing for binding code.
I have added the code in the build.gradle file and synced it.
buildFeatures {
viewBinding true
}
// Code for .kt file.
package com.nandini.android.workoutapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.nandini.android.workoutapp.databinding.ActivityBmiCalculatorBinding
class BmiCalculatorActivity : AppCompatActivity() {
private var binding: ActivityBmiCalculatorBinding?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityBmiCalculatorBinding.inflate(layoutInflater)
setContentView(binding?.root)
setSupportActionBar(binding?.toolbarBmi)
if(supportActionBar!=null){
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title="Calculate BMI"
}
binding?.toolbarBmi?.setNavigationOnClickListener {
onBackPressed()
}
}
}
The button to jump on this activity is in the activity_main.xml file with id : btnBmi , & intent code in the MainActivity.kt file.
// in the onCreate method of MainActivity.kt file.
binding?.btnBmi?.setOnClickListener{
val intent = Intent(this#MainActivity,BmiCalculatorActivity::class.java)
startActivity(intent)}
Step 1 change the Parent Theme in the style.xml/theme.xml-
parent="Theme.MaterialComponents.Light.NoActionBar"
Step 2 - Add toolbar in activity_bmi_calculator.xml
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="#drawable/ic_home_black_24dp" />
Step 3
class BmiCalculatorActivity
: AppCompatActivity() {
lateinit var binding: ActivityBmiCalculatorBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBmiCalculatorBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.toolbar.title = "Calculate BMI"
binding.toolbar.setNavigationOnClickListener {
onBackPressed()
}
}

Kotlin / Migration to View Binding

I was following YT video to make Quiz App, but in the end I got this error with binding:
The 'kotlin-android-extensions' Gradle plugin is deprecated.
Please use this migration guide
(https://goo.gle/kotlin-android-extensions-deprecation) to start working with View Binding (https://developer.android.com/topic/libraries/view-binding) and the 'kotlin-parcelize' plugin.
But when I add binding for instance to tv_name.text, i do get error that text is expecting Variable and everything the same with other binding parts.
Libraries part
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.example.quizapp.databinding.ActivityResultBinding
Code part
class ResultActivity : AppCompatActivity() {
private val binding by viewBinding(FragmentResultActivity::bind)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_result)
// Hide the status bar.
//window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
val userName = intent.getStringExtra(Constants.USER_NAME)
binding.tv_name.text = userName
val totalQuestions = intent.getIntExtra(Constants.TOTAL_QUESTIONS, 0)
val correctAnswers = intent.getIntExtra(Constants.CORRECT_ANSWERS, 0)
binding.tv_score.text = "Your Score is $correctAnswers out of $totalQuestions."
binding.btn_finish.setOnClickListener {
startActivity(Intent(this#ResultActivity, MainActivity::class.java))
}
}
}
You need to initialize the binding variable properly. Please use the below code:
class ResultActivity : AppCompatActivity() {
private lateinit var binding: ActivityResultBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityResultBinding.inflate(layoutInflater)
setContentView(binding.root)
// Hide the status bar.
//window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
val userName = intent.getStringExtra(Constants.USER_NAME)
binding.tv_name.text = userName
val totalQuestions = intent.getIntExtra(Constants.TOTAL_QUESTIONS, 0)
val correctAnswers = intent.getIntExtra(Constants.CORRECT_ANSWERS, 0)
binding.tv_score.text = "Your Score is $correctAnswers out of $totalQuestions."
binding.btn_finish.setOnClickListener {
startActivity(Intent(this#ResultActivity, MainActivity::class.java))
}
}
}
Since I do not have your resources with me available, I think you might have to change the naming of ActivityResultBinding. However, I am pretty sure this is it.
Happy Coding! :)
First you need to let android know that you are using view binding. so go to "Gradle Scripts" folder and open app level build.gradle(Module:nameOfProject) file and inside android property add this:
android {
// ------ VIEW BINDING SETTING ------
// this creates the binding object
buildFeatures{
viewBinding true
}
// after set up, click on "Sync Now"
}
then in MainActivity.kt:
class MainActivity : AppCompatActivity() {
// Initialize binding object. if ActivityMainBinding is not ready in menu click "Build/Make project"
private lateinit var binding:ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// view Bindig
// binding default layout inflater
binding= ActivityResultBinding.inflate(layoutInflater)
// inflate the root views which is Linear Layout, we access with .root
setContentView(binding.root)
// Set click listeners
....
}

I am not able to use viewbinding in activity in Kotlin android ,where I have also used companion object

package com.example.birthdayapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.birthdayapp.databinding.ActivityMainBinding
class birthdayActivity : AppCompatActivity() {
private lateinit var binding: birthdayActivity
//using companion object
companion object {
const val NAME_EXTRA="Name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//implementing view binding
binding= birthdayActivity.inflate(layoutInflater) //here red line shows below inflate
setContentView(binding.root) //here red line shows below root
//getting name from main activity
val name=intent.getStringExtra(NAME_EXTRA)
}
}
//MainActivity
package com.example.birthdayapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.birthdayapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater) //Here, I have successfully used viewbinding
setContentView(binding.root)
binding.birthdaybutn.setOnClickListener {
val name=binding.nam.editableText.toString()
val intent=Intent(this,birthdayActivity::class.java)
intent.putExtra(birthdayActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
}
I have successfully used viewbinding in MainActivity but when I tried to use viewbindig in birthdayActivity , i got red lines for ( inflate and root keyword ) ,because i have used companion object in birthdayActiviy whereas I have not companion object in MainActiviy

Android Studio - Kotlin Project's "Unsolved reference" problem

I add a textView in layout but when I'm trying to use Kotlin Android Extensions but I get Unsolved reference error on my TextView:
package com.normal.ff
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
fun first(a:Int, b:Int){
textView.text
}
Add import kotlinx.android.synthetic.main.activity_main.* at the top of your activity.
You are almost there, you just need a reference from layout file for that text view.
something like below
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById<TextView>(R.id.textView)
}
}
fun first (a:Int, b:Int){
val text = textView.text
}

Categories

Resources