为什么我的应用程序是用 Kotlin 编写的,当我按下'balls'按钮时会停止?



我的应用程序有两个屏幕,到目前为止在开发中运行正常。它已经有两个按钮("oss"one_answers"startGame"(可以正常工作。现在我又添加了一个名为"ball"的按钮,旨在更改4个文本视图的背景颜色("ball 1"到"ball 4"(。然而,按下一次没有任何作用,再次按下会导致应用程序停止,并显示消息";GC Clicker已停止";或";GC Clicker不断停止";。这是我的MainActivity.kt:的第一部分

package com.example.golfclicker
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import android.graphics.Color
import android.widget.Button
import android.content.Intent
import android.media.AudioManager
import android.media.SoundPool
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val soundPool:SoundPool?
// Link variables to xml views
val player1Name = findViewById<EditText>(R.id.PersonName1)
val handicap1 = findViewById<EditText>(R.id.hpInput1)
val player2Name = findViewById<EditText>(R.id.PersonName2)
val handicap2 = findViewById<EditText>(R.id.hpInput2)
val startGame = findViewById<Button>(R.id.startGame)
val toss = findViewById<Button>(R.id.toss)
val bkgnd1 = findViewById<TextView>(R.id.Background1)
val bkgnd1a = findViewById<TextView>(R.id.Background1a)
val bkgnd1b = findViewById<TextView>(R.id.Background1b)
val bkgnd2 = findViewById<TextView>(R.id.Background2)
val bkgnd2a = findViewById<TextView>(R.id.Background2a)
val bkgnd2b = findViewById<TextView>(R.id.Background2b)
val balls = findViewById<Button>(R.id.balls)
val ball1 = findViewById<TextView>(R.id.ball1)
val ball2 = findViewById<TextView>(R.id.ball2)
val ball3 = findViewById<TextView>(R.id.ball3)
val ball4 = findViewById<TextView>(R.id.ball4)
var priSec = true   // True:Primaries  False:Secondaries
var tossedP1 = ""      //String values to pass to Intent
var tossedHp1 =""
var tossedP2 = ""
var tossedHp2 =""
var tossed = false  //has Toss button been used?
//Set up soundpool and load the sound file
soundPool = SoundPool(2,AudioManager.STREAM_MUSIC,0)
val sound1 = soundPool.load(baseContext,R.raw.computer_keyboard,1)
val sound2 = soundPool.load(baseContext,R.raw.coins,1)
//Handle balls button click
balls.setOnClickListener {
if(priSec==true) {
with(ball1) { setBackgroundColor(Color.parseColor("button_blue")) }
with(ball2) { setBackgroundColor(Color.parseColor("red")) }
with(ball3) { setBackgroundColor(Color.parseColor("black")) }
with(ball4) { setBackgroundColor(Color.parseColor("yellow")) }
} else {
with(ball1) { setBackgroundColor(Color.parseColor("green")) }
with(ball2) {setBackgroundColor(Color.parseColor("pink")) }
with(ball3) {setBackgroundColor(Color.parseColor("brown")) }
with(ball4) {setBackgroundColor(Color.parseColor("white")) }
}
priSec = !priSec  //toggle primary/secondary
}
//Handle 'Toss' button click
toss.setOnClickListener {
var ran = (0..1).random()  //Toss coin
//        val ran = 1         //checking name-swapping

欢迎提出任何建议。

Color.parseColor(...)的工作方式与您想象的不同。你可以这样解析颜色:

Color.parseColor("#ff77bb")

换句话说,您必须通过彩色的十六进制代码

此行错误。。因为你没有定义的颜色

setBackgroundColor(Color.parseColor("button_blue") 

尝试

setBackgroundColor(Color.parseColor("#39b8db") 

context.getResources().getColor(android.R.color.white)

我终于找到了以下使用colors.xml中定义的颜色的作品。我不想在我的主代码中包含十六进制颜色,因为我可能想在以后编辑它们。

ball1.setBackgroundColor(getResources().getColor(R.color.button_blue))

感谢Niaj;这与你上次的建议相似。

最新更新