user15346567
user15346567

Reputation:

Why is it not recognized in main activity when I create text view?

I created a TextView inside activity_main.xml and specified an ID for it, but when I go into MainActivity and click on that ID, it does not recognize TextView, and what does the red line under everything look like? How should I solve this problem?

enter image description here

enter image description here

Upvotes: 0

Views: 1655

Answers (3)

steve bockelman
steve bockelman

Reputation: 1

As indicated in one of the posts above (for kotlin) I converted a project from java code to kotlin and had to manually add the line

apply plugin: 'kotlin-android-extensions'

to my gradle file example:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'

Edit note: I get the complier message The 'kotlin-android-extensions' Gradle plugin is deprecated. Please use this migration guide (https://goo.gle/kotlin-android-extensions-deprecation) to start working with View Binding (https://developer.android.com/topic/libraries/view-binding) and the 'kotlin-parcelize' plugin.

So evidently, Google is heading in a "view-binding" direction

Upvotes: 0

At gradle app

plugins {
   id 'kotlin-android-extensions'
}

or

apply plugin: 'kotlin-android-extensions'

then your Activity Import this

import kotlinx.android.synthetic.main.activity_main.*

Upvotes: 1

Jorge Gutiérrez
Jorge Gutiérrez

Reputation: 16

The mainActivity does not know where the txt_test is coming from so you have to give it the reference. I would recommend either use ViewBinding or use the old:

val txt = findViewById(R.id.the_id_of_your_textView)

Upvotes: 0

Related Questions