原因:java.lang.ArrayIndexOutOfBoundsException:length=5;在com.example.android.architecture.mainactivity.updateDisplay(mainactivity.kt:59)在com.example.android.architecture.mainactivity.oncreate(mainactivity.kt:44)
更改索引和类视图
class MainActivity : AppCompatActivity() {
private lateinit var dice: IntArray
private lateinit var headlineText: String
private val imageViews by lazy {
arrayOf<ImageView>(findViewById(R.id.die1),
findViewById(R.id.die1),
findViewById(R.id.die2),
findViewById(R.id.die3),
findViewById(R.id.die4),
findViewById(R.id.die5)
)
}
private val headline by lazy { findViewById<TextView>(R.id.headline) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
headlineText = savedInstanceState?.getString(HEADLINE_TEXT)
?: getString(R.string.welcome)
dice = savedInstanceState?.getIntArray(DICE_COLLECTION)
?: intArrayOf(6,6,6,6,6)
lifecycle.addObserver(MyLifeCycleObserver())
fab.setOnClickListener{
fabClickHandler()}
updateDisplay()
}
private fun fabClickHandler(){
dice = DiceHelper.rollDice()
headlineText = DiceHelper.evaluateDice(this,dice)
updateDisplay()
}
private fun updateDisplay() {
for (i in 0 until imageViews.size) {
val drawableId = when (dice[i]) {
1 -> R.drawable.die_1
2 -> R.drawable.die_2
3 -> R.drawable.die_3
4 -> R.drawable.die_4
5 -> R.drawable.die_5
6 -> R.drawable.die_6
else -> R.drawable.die_6
}
imageViews[i].setImageResource(drawableId)
}
headline.text = headlineText
}
override fun onSaveInstanceState(outState: Bundle?) {
outState?.putString(HEADLINE_TEXT,headlineText)
outState?.putIntArray(DICE_COLLECTION,dice)
super.onSaveInstanceState(outState)
}
}
我不知道为什么我的android应用程序没有运行
dice的大小为5,其中ImageViews有6个元素,因此循环运行6次,在第六次迭代中出现异常