I am trying to create dynamic TextView
by creating an array of TextViews
but getting error :
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
我的代码:
var txtViews = arrayOfNulls<TextView>(3)
for (i in txtViews.indices) {
txtViews = arrayOfNulls(i)
txtViews[i]?.textSize = 24.0F
txtViews[i]?.text = "Hello"
txtViews[i]?.setTextColor(ContextCompat.getColor(this,
R.color.colorAccent))
layout.addView(txtViews[i])
}
When i is 0 what is the meaning of
layout.addView(txtViews[i])
?! Putlayout.addView(txtViews[i]?)
您从未在数组中初始化过这些空引用:
这应该对您要实现的目标起作用: