kotlin/迁移到视图绑定

fcy6dtqo  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(356)

我一直在跟踪yt视频制作测验应用程序,但最终我在绑定时遇到了以下错误:
“kotlin android extensions”gradle插件已被弃用。
请使用本迁移指南(https://goo.gle/kotlin-android-extensions-deprecation)开始使用视图绑定的步骤(https://developer.android.com/topic/libraries/view-binding)以及“kotlin parcelize”插件。
但当我将绑定添加到例如tv_name.text时,我确实得到了一个错误,即text需要变量,而其他绑定部分的所有内容都是一样的。
图书馆部分

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

代码部分

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

j2qf4p5b1#

您需要正确初始化绑定变量。请使用以下代码:

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

由于我没有您的资源,我想您可能需要更改 ActivityResultBinding . 不过,我很肯定就是这样。
快乐编码!:)

相关问题