Question: App keeps stopping' error when the app is launched. scan the code and fix the errors that may be causing this problem. package com.example.diceroller import

App keeps stopping' error when the app is launched. scan the code and fix the errors that may be causing this problem.
package com.example.diceroller
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.rollButton)
rollButton.setOnClickListener {
rollDice()
}
}
private fun rollDice(){
val diceImage1: ImageView = findViewById(R.id.diceImage1)
val diceImage2: ImageView = findViewById(R.id.diceImage2)
val totalTextView: TextView = findViewById(R.id.totalTextView)
val dice1=(1..6).random()
val dice2=(1..6).random()
val total = dice1+ dice2
diceImage1.setImageResource(getDiceImage(dice1))
diceImage2.setImageResource(getDiceImage(dice2))
totalTextView.text = "Sum: $total"
}
private fun getDiceImage(diceValue: Int): Int {
return when (diceValue){
1-> R.drawable.dice_1
2-> R.drawable.dice_2
3-> R.drawable.dice_3
4-> R.drawable.dice_4
5-> R.drawable.dice_5
else -> R.drawable.dice_6
}
}
xml:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!