I get the message “Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Button?” in this line:
“binding.cheatButton.setOnClickListener {”
When I try to run the application I get the message:
" * Unresolved reference: cheatButton"
I have no idea how to fix this problem. Anyone?
this is my xml file:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/previous_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/previous_button"
app:icon="@drawable/arrow_left"
app:iconGravity="start"/>
<Button
android:id = "@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"
app:icon="@drawable/arrow_right"
app:iconGravity="end"/>
</LinearLayout>
This is my MainActivity.kt file:
binding.nextButton.setOnClickListener{
quizViewModel.moveToNext()
updateQuestion()
}
binding.previousButton.setOnClickListener{
//currentIndex = (currentIndex - 1) % questionBank.size
//val questionTextResId = questionBank[currentIndex].textResId
//binding.questionTextView.setText(questionTextResId)
quizViewModel.moveToPrevious()
updateQuestion()
}
binding.cheatButton.setOnClickListener {
val intent = Intent(this, CheatActivity::class.java)
startActivity(intent)
}