【问题标题】:lateinit property binding has not been initialized / Kotlinlateinit 属性绑定尚未初始化 / Kotlin
【发布时间】:2021-10-13 19:05:07
【问题描述】:

即使我已经初始化了绑定,我也遇到了绑定问题,但它无法识别它。我同时启用了 dataBinding 和 viewBinding。

这是我的代码片段

class QuizQuestionActivity : AppCompatActivity(), View.OnClickListener {

private lateinit var binding: ActivityQuizQuestionBinding
private var mCurrentPosition: Int = 1 // Default and the first question position
private var mQuestionsList: ArrayList<Question>? = null
private var mSelectedOptionPosition: Int = 0
private var mCorrectAnswers: Int = 0

// TODO (STEP 3: Create a variable for getting the name from intent.)
// START
private var mUserName: String? = null
// END

/**
 * This function is auto created by Android when the Activity Class is created.
 */
override fun onCreate(savedInstanceState: Bundle?) {
    //This call the parent constructor
    super.onCreate(savedInstanceState)

    binding = ActivityQuizQuestionBinding.inflate(layoutInflater)
    // This is used to align the xml view to this class
    setContentView(R.layout.activity_quiz_question)

    // TODO (STEP 4: Get the NAME from intent and assign it the variable.)
    // START
    mUserName = intent.getStringExtra(Constants.USER_NAME)
    // END

    mQuestionsList = Constants.getQuestions()

    setQuestion()

    binding.tvOptionOne.setOnClickListener(this)
    binding.tvOptionTwo.setOnClickListener(this)
    binding.tvOptionThree.setOnClickListener(this)
    binding.tvOptionFour.setOnClickListener(this)
    binding.btnSubmit.setOnClickListener(this)
}

我的 gradle 文件片段

    buildFeatures {
    dataBinding = true
    viewBinding = true
}

【问题讨论】:

    标签: android kotlin data-binding


    【解决方案1】:

    setContentView(R.layout.activity_quiz_question) 使用 setContentView(binding.root) 安装

    【讨论】:

    • 我之前尝试过,但我仍然得到同样的错误@anshul
    • 为什么同时启用数据绑定和视图绑定?
    • 在同一个布局文件上同时使用数据绑定和视图绑定会造成歧义并因此引发错误
    • 是的@Anshul,我也想过,你是说在gradle文件里吧?
    • 这可能是一个原因,为什么要同时使用?
    猜你喜欢
    • 2021-08-20
    • 2020-04-14
    • 1970-01-01
    • 2022-11-22
    • 2022-07-05
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多