Kotlin restart-button/android studio
Hej! Jag håller på att lära mig kotlin och ska göra ett spel där användaren ska gissa på ett nummer och om den förlorar eller vinner ska det finnas en 'Restart'-button. Först satte jag bara att när användaren klickar på 'restart' så ska buttonClick() kallas, men när jag klickar på 'restart' kastas jag ut ur appen. Tips?
Kotlin-fil:
class MainActivity : AppCompatActivity() {
var numberToGuess = Random.nextInt(1,101)
var numberOfGuesses = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun buttonClick(view: View){
val tv = findViewById<TextView>(R.id.resultView)
val et = findViewById<EditText>(R.id.userInput)
val enterButton = findViewById<Button>(R.id.enterButton)
val userGuess = et.text.toString().toInt()
val restartButton = findViewById<Button>(R.id.restartButton)
if(userGuess < 1 || userGuess > 100 ){
tv.text = "Your guess must be between 1 and 100!"
et.text.clear()
}
else{
when {
userGuess < numberToGuess -> {
tv.text = "Oh no! The number is higher!"
et.text.clear()
numberOfGuesses++
}
userGuess > numberToGuess -> {
tv.text = "Oh no! The number is lower!"
et.text.clear()
numberOfGuesses++
}
else -> {
if (numberOfGuesses == 1){
tv.text = "Congrats! You won the game after $numberOfGuesses guess. The correct number was $numberToGuess!"
} else {
tv.text = "Congrats! You won the game after $numberOfGuesses guesses. The correct number was $numberToGuess!"
}
et.text.clear()
enterButton.isVisible = false
restartButton.isVisible = true
}
}
}
if(numberOfGuesses >= 2) {
tv.text = "Oh no! You lost the game after $numberOfGuesses guesses. The correct number was $numberToGuess!"
enterButton.isVisible = false
restartButton.isVisible = true
}
}
}
XML-fil:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Binary Search"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.113" />
<EditText
android:id="@+id/userInput"
android:layout_width="395dp"
android:layout_height="47dp"
android:hint="Please enter a number between 0 and 100"
android:textColorHint="#d3cdc0"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/enterButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="Enter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.456" />
<TextView
android:id="@+id/resultView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enterButton"
app:layout_constraintVertical_bias="0.221" />
<Button
android:id="@+id/restartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:text="Restart"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/resultView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/userInput"
app:layout_constraintVertical_bias="0.349" />
Tack på förhand!