I’m starting to do a dice game but when i click the button the app it will show keep stopping, please help and let me the problem, thanks a lot
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity”>
<TextView
android:id="@+id/roll_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll"
android:textSize="28dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dice_image_view2" />
<ImageView
android:id="@+id/dice_image_view3"
android:layout_width="124dp"
android:layout_height="133dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.972"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.323"
app:srcCompat="@drawable/d6" />
<ImageView
android:id="@+id/dice_image_view2"
android:layout_width="124dp"
android:layout_height="133dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.323"
app:srcCompat="@drawable/d6" />
<ImageView
android:id="@+id/dice_image_view"
android:layout_width="124dp"
android:layout_height="133dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/dice_image_view2"
app:layout_constraintHorizontal_bias="0.516"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.323"
app:srcCompat="@drawable/d6" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:backgroundTint="@color/black"
android:onClick="onRollButtonClick"
android:text="Play"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Main Activity.kt**
package imat6015.s20216995.dicegame
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.view.View
class MainActivity : AppCompatActivity() {
private lateinit var roll_text: TextView
private lateinit var dice_image_view: ImageView
private lateinit var dice_image_view2: ImageView
private lateinit var dice_image_view3: ImageView
private lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
roll_text = findViewById(R.id.roll_text)
dice_image_view = findViewById(R.id.dice_image_view)
dice_image_view2 = findViewById(R.id.dice_image_view2)
dice_image_view3 = findViewById(R.id.dice_image_view3)
button = findViewById(R.id.button)
button.setOnClickListener {
val animation: Animation = AnimationUtils.loadAnimation(applicationContext, R.anim.animation)
dice_image_view.startAnimation(animation)
dice_image_view2.startAnimation(animation)
dice_image_view3.startAnimation(animation)
rollDice()
}
}
fun onRollButtonClick(view: View) {
val animation: Animation = AnimationUtils.loadAnimation(applicationContext, R.anim.animation)
dice_image_view.startAnimation(animation)
dice_image_view2.startAnimation(animation)
dice_image_view3.startAnimation(animation)
rollDice()
}
private fun rollDice() {
val randomInt1 = (1..6).random()
val randomInt2 = (1..6).random()
val randomInt3 = (1..6).random()
val drawableImage1 = getDrawableImage(randomInt1)
val drawableImage2 = getDrawableImage(randomInt2)
val drawableImage3 = getDrawableImage(randomInt3)
dice_image_view.setImageResource(drawableImage1)
dice_image_view2.setImageResource(drawableImage2)
dice_image_view3.setImageResource(drawableImage3)
val roundScore = calculateRoundScore(randomInt1, randomInt2, randomInt3)
val totalScore = roundScore + getCurrentTotalScore()
roll_text.text = "Round Score: $roundScore\n Total Score: $totalScore"
}
private fun getDrawableImage(diceValue: Int): Int {
return when (diceValue) {
1 -> R.drawable.d1
2 -> R.drawable.d2
3 -> R.drawable.d3
4 -> R.drawable.d4
5 -> R.drawable.d5
else -> R.drawable.d6
}
}
private fun calculateRoundScore(dice1: Int, dice2: Int, dice3: Int): Int {
return when {
dice1 == 1 && dice2 == 1 && dice3 == 1 -> 200
dice1 == dice2 && dice2 == dice3 -> 100
dice1 == 1 && dice2 == 1 || dice1 == 1 && dice3 == 1 || dice2 == 1 && dice3 == 1 -> 50
dice1 == dice2 || dice1 == dice3 || dice2 == dice3 -> 20
dice1 == 1 || dice2 == 1 || dice3 == 1 -> 10
else -> -10
}
}
private fun getCurrentTotalScore(): Int {
val currentScoreText = roll_text.text.toString()
val totalScoreIndex = currentScoreText.lastIndexOf(":") + 1
return currentScoreText.substring(totalScoreIndex).trim().toInt()
}
}