簡體   English   中英

無法創建抽象類的實例Random kotlin

[英]Cannot create an instance of an abstract class Random kotlin

我試圖在Android Studio上制作Dice Roller應用程序,但是當我將此Random()放置時,出現了一個錯誤,說“無法創建抽象類的實例”,請幫幫我

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()
    }
}

我希望每當我單擊按鈕時輸出在6到1之間是隨機的

Kotlin中的Random是抽象的,但是它提供了一些可以直接使用的靜態方法,例如:

Random.nextInt(6)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM