Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

find and correct the errors in the dice roller kt code below. The code was written but when the app is launched an error app

find and correct the errors in the dice roller kt code below. The code was written but when the app is launched an error "app has stopped " keep popping up... the code is as follows:
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?){
this.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()
}
}
private fun setContentView(activityMain: Any){
TODO("Not yet implemented")
}
@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))
this.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
}
}
}
private fun Any.onCreate(savedInstanceState: Bundle?){
}
The xml code is as follows:

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions