【问题标题】:I wish To show random Text in my splash screen's text view using kotlin我希望使用 kotlin 在我的初始屏幕的文本视图中显示随机文本
【发布时间】:2023-01-01 03:05:05
【问题描述】:

我正在尝试在我的初始屏幕中 [使用 kotlin] 显示数组中的随机文本 [我想是在 MainActivity 启动之前,] 我试过了,

val thoughts = arrayOf("Trading Made Easy!","Where Smart People Trade","For Smart Traders Like You!")
    fun getRandomQuote(): String {
        val randomValue = kotlin.random.Random.nextInt(thoughts.size)
        return thoughts[randomValue]
    }
    var thought = getRandomQuote().toString()
    val textView = findViewById<TextView>(R.id.rand)
    textView.setText(thought)

但这会使应用程序自动关闭而不是显示启动画面。 无论如何要解决它?或任何其他替代方式?

【问题讨论】:

  • 如果您崩溃了,请从日志中发布堆栈跟踪。它会告诉你崩溃的原因。

标签: android kotlin android-layout


【解决方案1】:
Instead of using thoughts.size,  you should use thoughts.size-1

否则你 使用时可能会出现 ArrayIndexOutOfBoundsException 想法[随机值]
有趣的 getRandomQuote(): 字符串 { val 随机值 = kotlin.random.Random.nextInt(想法。大小) 返回想法[randomValue] }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2011-06-06
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多