'Kotlin-Android First App Unresolved Reference TextView Button etc
I'm new to Android & Kotlin development.
I wanted to get started with a simple "Hello World", but am already running into problems.
I added a Textview to my MainActivity and want to set an onClick listener to change the text of a TextView I dragged into the activity.
The compiler is now complaining that 'TextView' is an unresolved reference (it does the same with Buttons etc.).
I then added a kotlinx import as suggested by a website, but this fails to solve anything. Code sample below, anything with an asterisk as a line comment was added by me.
package com.example.my.mynewapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.fragmentX.view.* // *
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView: TextView = findViewById(R.id.testView) as TextView // *
textView.setOnClickListener { // *
textView.text = "You clicked me! You flipping clicked me!" // *
} // *
}
}
Would anyone have any idea what's going on?
Solution 1:[1]
It should have been automatically imported, but there should be
import android.widget.<WhateverIsMissing>
Replace WhateverIsMissing with the reference that is unresolved
Solution 2:[2]
if you use Android Studio v.4.2.1 you have to add this line in (build.gradle) file like this:-
id 'kotlin-android-extensions'
Solution 3:[3]
You are inflating activity_main.xml
in your class.
Does this TextView
belong to the above layout?
If it does then you don't need findViewById()
Just add to your imports:
import kotlinx.android.synthetic.main.activity_main.*
and not:
import kotlinx.android.synthetic.main.fragmentX.view.*
then use testView
(this is the id of the TextView
unless it's a typo) anywhere in your activity class.
Solution 4:[4]
One more thing to check if your imports seem complete:
In case you have defined more than one layout for your activity, fragment, etc. make sure that classes of corresponding controls match across layouts. This is the most common situation where I see this error message.
Solution 5:[5]
When you add a new view (in this case TextView
), the IDE will default its id
to textView
. The IDE will increment the id
every time you add a new view of the same kind (textView2
, textView3
, and so on) even when you already deleted the previous views. For example:
// Capture the layout's TextView and set the string as its text
val textView = findViewById<TextView>(R.id.textView).apply {
text = message
}
If you get Unresolved reference: textView
from the R.id.textView
, check the corresponding XML file. Specifically, the Component Tree:
To change the id
, select the view and in the Attributes window, change the id
:
Solution 6:[6]
add apply plugin: 'kotlin-android-extensions' in the build.gradle by double shift to search and write "build" then add the plugin in the dependencies section then import option should appear for you
Solution 7:[7]
- Under the project folder there are gradle scripts
- Open the build.gradle (module:your app name)
- add 'id-android-extensions' into the plugins
- then sync it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | frogstair |
Solution 2 | Majid Hajibaba |
Solution 3 | forpas |
Solution 4 | jerry |
Solution 5 | M Imam Pratama |
Solution 6 | 7ossam |
Solution 7 | MaxV |