[英]Kotlin / lateinit property binding has not been initialized
I working on Quiz App from YT tutorial and I am facing the issue with lateinit property binding has not been initialized .我正在使用 YT 教程中的测验应用程序,但遇到了lateinit 属性绑定尚未初始化的问题。
Here its the part where i have initialized binding, i get error in bolded words: binding = MessageItemBinding .inflate( inflater )在这里它在那里我有初始化结合的部分,我得到错误的粗体字:绑定= MessageItemBinding .inflate(充气)
Code part代码部分
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
private var mUserName: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
binding = DataBindingUtil.inflate(inflater, R.layout.message_item, container, false)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_question)
mUserName = intent.getStringExtra(Constants.USER_NAME)
mQuestionsList = Constants.getQuestions()
setQuestion()
binding.tvOptionOne.setOnClickListener(this)
...
}
Here's my graddle.这是我的毕业证。 Maybe you would what's missing
也许你会缺少什么
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
...
buildFeatures {
viewBinding true
}
kapt {
generateStubs = true
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.google.android.material:material:1.1.0-alpha10'
kapt 'com.android.databinding:compiler:2.0.0-rc1'
}
You are not properly setting up the binding I assume.您没有正确设置我假设的绑定。 The following code seems the issue
以下代码似乎是问题
binding = DataBindingUtil.inflate(inflater, R.layout.message_item, container, false)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz_question)
Instead you can simply let it be like this.相反,您可以简单地让它变成这样。 You are currently setting up binding of some other layout to your activity.
您当前正在设置将其他一些布局绑定到您的活动。
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_quiz_question)
Refer here for more: https://developer.android.com/topic/libraries/data-binding请参阅此处了解更多信息: https : //developer.android.com/topic/libraries/data-binding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.