So im new to programming and wanted to get involved. I started to learn Kotlin and I have created a simple dice rolling application in which I added new features as I have learned. However I was following a tutorial and the person pressed File> Invalidade Caches/restart and I followed, after that NOTHING works anymore and im completely stumped. All references in my Mainactivity is unresolved and I cant get it to work.
My build.gradle contains apply plugin: 'kotlin-android-extensions'
and my build.gradle project contains
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
That is what my research has gotten me to but now I'm stuck. :(
This is my code:
package com.example.diceroller
import android.R
import android.media.MediaPlayer
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.*
class MainActivity : AppCompatActivity() {
lateinit var mp: MediaPlayer
lateinit var diceImage: ImageView
var setDiceInt = 6
private var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton: Button = findViewById(R.id.roll_button)
val rollList: TextView = findViewById(R.id.roll_list)
val fourButton: Button = findViewById(R.id.d4)
val sixButton: Button = findViewById(R.id.d6)
val eightButton: Button = findViewById(R.id.d8)
val tenButton: Button = findViewById(R.id.d10)
val twelveButton: Button = findViewById(R.id.d12)
val twentyButton: Button = findViewById(R.id.d20)
rollList.movementMethod = ScrollingMovementMethod()
// mp = MediaPlayer.create(this, R.raw.) to be added later
fourButton.setOnClickListener {
setDiceInt = 4
Toast.makeText(this, "$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
sixButton.setOnClickListener {
setDiceInt = 6
Toast.makeText(this,"$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
eightButton.setOnClickListener {
setDiceInt = 8
Toast.makeText(this,"$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
tenButton.setOnClickListener {
setDiceInt = 10
Toast.makeText(this,"$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
twelveButton.setOnClickListener {
setDiceInt = 12
Toast.makeText(this,"$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
twentyButton.setOnClickListener {
setDiceInt = 20
Toast.makeText(this,"$setDiceInt sided picked", Toast.LENGTH_SHORT).show()
}
rollList.text = ""
rollButton.text = "Lets roll!"
rollButton.setOnClickListener {
rollDice()
}
diceImage = findViewById(R.id.result_image)
}
fun rollDice() {
val resultText: TextView = findViewById(R.id.result_text)
val rollList: TextView = findViewById(R.id.roll_list)
val randomInt = Random().nextInt(setDiceInt) +1
val drawableResource = when (randomInt) {
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
}
val scream = if (randomInt != 1) {
} else {
mp.start()
}
diceImage.setImageResource(drawableResource)
resultText.text = randomInt.toString()
rollList.append(setDiceInt.toString() + " sided dice hit: " + randomInt.toString() + "\n")
}
}
Open Recent, go toFile -> Open, and to the directory). Works for me in similar scenarios. - omz1990