Just as the title says, it crashes on startup without a LOGCAT output and no errors at around 6.13
Here’s my MainActivity
package com.jacobjumper.geoquiz
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
private const val KEY_INDEX = "index"
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: Button
private lateinit var prevButton: Button
private lateinit var cheatButton: Button
private lateinit var questionTextView: TextView
private lateinit var quizViewModel: QuizViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
savedInstanceState?.putInt(KEY_INDEX, quizViewModel.currentIndex)
setContentView(R.layout.activity_main)
quizViewModel = ViewModelProvider(this)[QuizViewModel::class.java]
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
cheatButton = findViewById(R.id.cheat_button)
nextButton = findViewById(R.id.next_button)
prevButton = findViewById(R.id.back_button)
questionTextView = findViewById(R.id.question_text)
trueButton.setOnClickListener {
checkAnswer(true)
}
falseButton.setOnClickListener {
checkAnswer(false)
}
nextButton.setOnClickListener {
quizViewModel.moveToNext()
updateQuestion()
}
prevButton.setOnClickListener {
quizViewModel.moveToBack()
updateQuestion()
}
cheatButton.setOnClickListener {
// Start intent
val answerIsTrue = quizViewModel.currentQuestionAnswer
val intent = CheatActivity.newIntent(this@MainActivity, answerIsTrue)
startActivity(intent)
}
updateQuestion()
}
private fun checkAnswer(userAnswer: Boolean){
val correctAnswer = quizViewModel.currentQuestionAnswer
val messageResId = if (userAnswer == correctAnswer) {
R.string.correct_toast
} else {
R.string.incorrect_toast
}
Toast.makeText(
this,
messageResId,
Toast.LENGTH_SHORT
).show()
}
private fun updateQuestion() {
val questionTextResID = quizViewModel.currentQuestionText
questionTextView.setText(questionTextResID)
}
}
and here’s my CheatActivity
package com.jacobjumper.geoquiz
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import org.w3c.dom.Text
import java.security.AccessControlContext
private const val EXTRA_ANSWER_IS_TRUE =
"com.jacobjumper.geoquiz.answer_is_true"
class CheatActivity : AppCompatActivity() {
private lateinit var answerTextView: TextView
private lateinit var showAnswerButton: Button
private var answerIsTrue = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cheat)
answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)
answerTextView = findViewById(R.id.answer_text_view)
showAnswerButton = findViewById(R.id.answer_button)
showAnswerButton.setOnClickListener {
val answerText = when {
answerIsTrue -> R.string.true_button
else -> R.string.false_button
}
answerTextView.setText(answerText)
}
}
companion object {
fun newIntent(packageContext: Context, answerIsTrue: Boolean): Intent {
return Intent(packageContext, CheatActivity::class.java).apply {
putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue)
}
}
}
}
This is an older book so I’m willing to bet something is depreciated somewhere. Please assist. Thank you.