Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions