Casey
Casey

Reputation: 552

First app - Random.nextInt() unresolved reference

I have followed this tutorial for building an android app. https://www.youtube.com/watch?v=EOfCEhWq8sg

It goes well up until the end, where we generate a random val using Random.nextInt(). In Android Studio (3.3), I get an error :

an unresolved reference to nextInt.

I have made sure I am using the right spelling. This program uses Kotlin, and I even imported java.util.* to get the Random library imported.

Please help me out, it is very discouraging not being able to get such a simple application built.

Thanks.

Upvotes: 3

Views: 5330

Answers (1)

Sergio
Sergio

Reputation: 30705

If you use Random class from java.util package you need to create an instance of Random class. Instances in Kotlin are created using NameOfClass() syntax:

java.util.Random().nextInt()

If you use Random class from kotlin.random package you don't need to create an instance of the class:

kotlin.random.Random.nextInt()

Upvotes: 13

Related Questions