Cannot create an instance of an abstract class Random kotlin

waqar mughal :

I was trying to make a Dice Roller app on Android Studio but when i put this Random() i get an error by saying that "Cannot create an instance of an abstract class" Please Help me

package com.example.diceroller

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val rollbutton: Button = findViewById(R.id.roll_button)
        rollbutton.setOnClickListener(
            View.OnClickListener { diceRoll() }


        )
    }
    private fun diceRoll() {
        val resultText: TextView = findViewById(R.id.result_text)
        val randomInt = Random().nextInt(6) + 1
        resultText.text = randomInt.toString()
    }
}

I Expect the output to be random between 6 to 1 whenever i click button

devgianlu :

Random in Kotlin is abstract, but it provides some static methods that can be used directly, like so:

Random.nextInt(6)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=311051&siteId=1