android – How to pass a view id in Kotlin?-ThrowExceptions

Exception or error:

I’m trying to use Kotlin on Android. I need to pass a viewId to a function, so that the function can display a text view.

So, I tried to create a function with inputs: string (to be displayed), and the id (for the location of where it will be displayed):


ShowHello("Hello world",

The function:

    fun ShowHello(toshow:String, thisId:id ){ textView.text=toshow }

But, the thisId:id is failing with “Unresolved reference: id”?

Any help would be really appreciated.

How to solve:

If you want to pass a resource ID (like a view ID), you can use an Int with the @IdRes annotation to clarify that only resource (res) ID’s should be passed in that position:

fun showHello(displayText: String, @IdRes viewId: Int)

To understand why this works, look into how resources are referenced. Each resource ID is mapped to a constant. When you call an ID in the format, you get back an integer that references that resource.

Leave a Reply

Your email address will not be published. Required fields are marked *