Creating an array of buttons
val buttons = arrayOf(spot0, spot1, spot2, spot3, spot4, spot5, spot6)
Log.v("array", "spot0=" + buttons[0])
images.shuffle()
val randomnumber = (0..5).random()
buttons[0].setBackgroundResource(images[randomnumber])
Gets this error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setBackgroundResource(int)' on a null object reference
and this log output:
V/array: spot0=null
This is Kotlin so I shouldn't have to call findViewById from what I can tell.
apply plugin: 'kotlin-android-extensions' is added to my build.gradle
Button is setup in xml:
<ImageButton
android:id="@+id/spot0"
android:text="zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="131dp"
android:layout_marginLeft="131dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="131dp"
android:layout_marginRight="131dp"
android:background="@drawable/heart"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.565"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I'm attempting to randomize my button images and attempting to do it via an array. How can I go about this?
buttonsarray?setContentView(R.id.your_layout)method before initializing the array?