2

I have the following method in java and works well in my code. But I just want to rewrite my activity to kotlin. My java methods:

private void testFunction() {
    ImageView[] pics;
    int count = 6;

    pics = new ImageView[count];
}

By converting it automatically with Android studio the following fun is produced but with error:

private fun testFunction() {
    val pics: Array<ImageView>
    val count = 6

    pics = arrayOfNulls(count)
}

How can I archive the same result?

Error

enter image description here

2 Answers 2

2
private fun testFunction() {
    val pics: Array<ImageView?> = arrayOfNulls(6)

    // TODO the rest of your test
}
Sign up to request clarification or add additional context in comments.

Comments

1

Or even shorter:

val pics = arrayOfNulls<ImageView?>(6)

This takes advantage of Kotlin's type inference and avoids redundancy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.