Tomee
Tomee

Reputation: 139

Kotlin / Migration to View Binding

I was following YT video to make Quiz App, but in the end I got this error with binding:

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))
        }
      }
   }

Upvotes: 1

Views: 876

Answers (2)

Yilmaz
Yilmaz

Reputation: 49182

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
      ....
}

Upvotes: 0

Akshat Tiwari
Akshat Tiwari

Reputation: 389

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! :)

Upvotes: 1

Related Questions