Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When the app launches , the app keeps crashing. Find the errors in the code below to fix the crashing issue. kt code: package com.example.thediceroller

When the app launches ,the app keeps crashing. Find the errors in the code below to fix the crashing issue.
kt code:
package com.example.thediceroller
import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random
class MainActivity: AppCompatActivity(){
private lateinit var ivDice1: ImageView
private lateinit var ivDice2: ImageView
private lateinit var tvSum: TextView
private lateinit var btnRoll: Button
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ivDice1= findViewById(R.id.ivDice1)
ivDice2= findViewById(R.id.ivDice2)
tvSum = findViewById(R.id.tvSum)
btnRoll = findViewById(R.id.btnRoll)
btnRoll.setOnClickListener {
rollDice()
}
}
@SuppressLint("SetTextI18n")
private fun rollDice(){
val dice1= Random.nextInt(1,7)
val dice2= Random.nextInt(1,7)
val sum = dice1+ dice2
ivDice1.setImageResource(getDiceDrawable(dice1))
ivDice2.setImageResource(getDiceDrawable(dice2))
tvSum.text = "Sum: $sum"
}
private fun getDiceDrawable(diceValue: Int): Int {
return when (diceValue){
1-> R.drawable.dice1
2-> R.drawable.dice2
3-> R.drawable.dice3
4-> R.drawable.dice4
5-> R.drawable.dice5
else -> R.drawable.dice6
}
}
}
xml code:

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

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

Th ey told me Id have to write a lett er. Whos got time for that?

Answered: 1 week ago