Chatron
Chatron

Reputation: 23

Android Studio ActivityMain.kt not finding IDs of TextView, Buttons etc from activity-main.xml

I'm trying to learn programming. Using Android Studio I have created an TextView and and Button in activity_main.xml and now I'm trining to use this two in MainActivity.kt but Android Studio cannot find the IDs of my Textview nor of my button. Am I doing something wrong?

Thanks for your help and suggestions.

OS ManjaroLinux Android Studio v. 4.1.1 installed by Flatpack using Kotlin as programming language

Images of my problem

Button declaration activity_main.xml

TextView declaration activity_main.xml

MainActivity.kt error

Upvotes: 2

Views: 1652

Answers (1)

Saurabh Dhall
Saurabh Dhall

Reputation: 44

Refer ids of components using R.id.*. For example, if the id is defined by :

android:id="@+id/buttonTxt"

Then refer it by :

val button = findViewById<Button>(R.id.buttonTxt)

The same applies for any resources like strings (R.string.*), colors, dimens etc.

Upvotes: 2

Related Questions