0
votes

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")


    }

}
1
Try to close and re-import the project instead of invalidate and restart. A lot of times that fixes these sort of broken issues (don't just open it from the Open Recent, go to File -> Open, and to the directory). Works for me in similar scenarios. - omz1990
You could try: 0- BACKUP THE ENTIRE PROJECT 1- Invalidade caches 2- close the project 3- close Android Studio IDE 4- delete the .idea directory 5- delete all .iml files 5- Run Android Studio IDE and reopen the project - Red wine
Can you post the errors that you are getting? - Stack Underflow
None of these suggestions worked unfortunately. This is the error message in the build: ``` org.gradle.internal.exceptions.LocationAwareException: Execution failed for task ':app:compileDebugKotlin'. at org.gradle.initialization.exception.DefaultExceptionAnalyser.transform(DefaultExceptionAnalyser.java:99) ``` Etc.. Its a very long error. I also get Redeclaration: MainActivity and Unresolved reference on all the buttons, textfields etc and activity_main. - Dan

1 Answers

0
votes

Please try to clean below cache folders to re-download the libraries -

c:\Users\<user>\.gradle\caches\
c:\Users\<user>\.AndroidStudio3.x\system\caches\