- Why Do I Get «Unresolved Reference» Error for My View’s Name/Id When I Type It in Kotlin
- Why do I get unresolved reference error for my view’s name/ID when I type it in Kotlin?
- Kotlin Unresolved reference: btn_start but i define it in my layout
- Why does it still say Unresolved reference: button even when I have button id in xml?
- “Unresolved reference” in MainActivity.kt calling a TextView?
- Unresolved reference in kotlin with id and activity_main
- Kotlin Error at runtime — Unresolved reference Object ID
- Android Studio 3.1.3 — Unresolved reference: R — Kotlin
- Unresolved Reference: findViewById in Kotlin
- Unresolved Reference: findViewById in Kotlin
- “Unresolved reference” in MainActivity.kt calling a TextView? [duplicate]
- Why it’s unresolved reference error for calling setBackgroundResource() on an ‘ id ‘ in Mainactivity.kt?
Why Do I Get «Unresolved Reference» Error for My View’s Name/Id When I Type It in Kotlin
Why do I get unresolved reference error for my view’s name/ID when I type it in Kotlin?
The ability to refer to a view directly by it’s ID/name in Kotlin is called «synthetic properties» and it is a feature of a project plugin called Kotlin Android Extensions.
Google and JetBrains decided to deprecate Kotlin Android Extensions, meaning they no longer support it, and discourage you from using it. Ever since it was deprecated, when you create a new project in Android Studio, the plugin is no longer included in the new project. Therefore, synthetic properties are not supported in new projects and you will get an «unresolved reference» error if you try to use it.
Tutorials written between 2017 and 2020 often make use of this feature, and if they haven’t been updated, they probably don’t even mention it by name, because it was taken for granted to be an included plugin in new projects.
Google explained the reasons for deprecating it in this blog post, with these key reasons:
- They pollute the global namespace
- They don’t expose nullability information
- They only work in Kotlin code
The quick and easy way to get your view reference is to use findViewById . The type of View should go inside the brackets <> . In an Activity, it looks like this:
override fun onCreate(savedInstanceState: Bundle?) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nameTextView = findViewById(R.id.nameTextView)
// Now you can refer to the view using the variable
nameTextView.setText(R.string.hello_world)
>
In a Fragment, you would probably be working with the view in the onViewCreated function, so you must call findViewById on the parent view. (If you need to access it elsewhere in the Fragment, use requireView() instead of view .
override fun onViewCreated(view: View, savedInstanceState: Bundle?) super.onViewCreated(view, savedInstanceState)
val nameTextView = view.findViewById(R.id.nameTextView)
//.
>
findViewById is probably the best option for now if you just want to complete your tutorial that was written before Kotlin Android Extensions was deprecated.
However, using findViewById can be tedious, and it is also error prone, because it won’t warn you if you are searching for a view that isn’t in the current layout. If you do, it will crash at runtime. For this reason, Google recommends using View Binding. There are a few steps to get started with view binding, but once you set it up, it is a cleaner option than findViewById . The official instructions are here.
Finally, if you really don’t care that Kotlin Android Extensions is deprecated and want to use it anyway, it currently still works OK, but you have to add the plugin to your new project to enable it. (Beware this will no longer work in Kotlin 1.8 and up.) To do that, open the build.gradle file for your app module. At the top in the plugins block, you can add a line for kotlin-android-extensions , like this:
plugins id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
>
Then press the «Sync project with Gradle files» button in the toolbar to enable it.
Kotlin Unresolved reference: btn_start but i define it in my layout
Kotlin Android Extensions is deprecated, which means that using Kotlin
synthetics for view binding is no longer supported.
Please refer to the official documentation here
Why does it still say Unresolved reference: button even when I have button id in xml?
val button = findViewById(R.id.button)
“Unresolved reference” in MainActivity.kt calling a TextView?
I think you need to define on MainActivity.kt like this
val ShowMeIt= findViewById(R.id.ShowMeIt)
Unresolved reference in kotlin with id and activity_main
In my case removing import android.R solved the issue.
Kotlin Error at runtime — Unresolved reference Object ID
Looks like you’re trying to use synthetic view binding. Your project needs to have the kotlin-android-extension plugin enabled in build.gradle like:
apply plugin: 'kotlin-android-extensions'
After that you should be able to import your btnDatePicker .
Note that kotlin-android-extensions and synthetic view binding is deprecated so you are probably better off with Jetpack view binding. See https://developer.android.com/topic/libraries/view-binding/migration
Android Studio 3.1.3 — Unresolved reference: R — Kotlin
The issue can be caused by many factors,
- as mentioned by martomstom in this Answer the issue is sometimes caused by com.android.tools.build:gradle version, changing it’s version to a more stable one would solve the problem: for example: com.android.tools.build:gradle:3.4.0-alpha02 with com.android.tools.build:gradle:3.2.1
- Also, having libraries from the same group, but with different versions may cause the problem or even more runtime errors. use the exclude group method like the following : implementation(‘com.squareup.picasso:picasso:2.71828’) < exclude(group: 'com.android.support') >in this case, picasso library uses android.support components, the android library version used in picasso is different than the one you’re currently using in your app, so in order to solve this issue, we have to exclude it completely from its sub library and class groups.
- It can also happen by the mismatch of resources and code, including this importation line in your activity may solve the problem too : import com.package.name.R
- Sometimes it can happen because of the IDE, performances or memory.. Cleaning the project from time to time may save you some time, on Android Studio it would be something like this : Build -> Clean Project / Rebuild Project — Cleaning IDE cash also helps with performance and memory, on Android Studio it would look like this : File-> Invalidate Chases/ Restart -> Invalidate Cashes and Restart
- I noticed that this problem happens to me the most of the time when importing new resources, Using prohibited characters in their names would fire the error, such as . , , — , UpperCase or special Letters
- And as a suggestion , if you’re using Kotlin, i really recommend using Kotlin extensions in your activity such as : import kotlinx.android.synthetic.main.activity_page.* or if you’re using a custom view : kotlinx.android.synthetic.main.view_layout.view.*
after that, in onCreat() method of an activity , you’ll only have to call the id, for example : my_edit_text_ID.text = «Kotlin Dbest!» , or from a custom view : mCostumView.my_edit_text_ID.text = «Kotlin Dbest!»
- I have faced this issue againe and the problem was the » R » library was imported from 2 different sources :
com.android.R
com.example.package.R
As tobltobs mentioned in the comments section: » Most of the time the problem is caused by another error which prevents the build system from creating generated sources. To find the root cause look at the gradle log (the «toggle view» icon below of the green hammer in the Build output) and look for errors unrelated to R or BuildConfig (also generated). If there is no other error left and the problem with R persists then maybe something of this list might help. «
As Patrick Beagan mentioned, Kotlin extensions are now deprecated — I’d advise using ViewBinding instead
Unresolved Reference: findViewById in Kotlin
However id’s with same name has been declared in XML file. All used elements in your code gets automatically found and assigned to same variable name as ids in layout(xml) by kotlin.
Unresolved Reference: findViewById in Kotlin
I’m still a beginner in Kotlin having learnt only the basic working of kotlin, I am unable refer to any android widget or change it’s state in Android Studio whether it’s TextView or CheckBox or RadioBox.
Same Unresolved Reference errors for findViewById in all cases.
I don’t know what is it that I am doing wrong, even java conversion outputs the same errors.
It seems this is the easiest way to get rid of findViewById ()
Go to your Build.Gradle (Module: app)
apply plugin: 'kotlin-android-extensions'
After that come to your Activity file Say it many be MainActivity.kt
There import To import single view
import kotlinx.android.synthetic.main..;
import kotlinx.android.synthetic.main..*;
it is in activity_main layout then import as
import kotlinx.android.synthetic.main.activity_main.forwardBox;
so either in your function or class use it directly
forwardBox.isChecked = false
In Kotlin you do not need to use findViewById , Simply use id ForwardBox from kotlinx.android.synthetic. . All used elements in your code gets automatically found and assigned to same variable name as ids in layout(xml) by kotlin.
Anyway, consider to define your IDs in lowercase separed by underscores, for instance:
val checkBox = findViewById(R.id.forward_box) as CheckBox
I have faced exactly the same issue, the problem is they have upgraded everything to view binding , Try to use it as a private function inside your main class class MainActivity : AppCompatActivity() — there it worked for me.
How to get rid of `Unresolved reference: NavController`, if the project can be built, then it may be a Kotlin/JVM mismatch. But if you find a build error, consider checking your dependencies. if the above solution does not fix the error, try to close android studio completely and then restart it. To add to Dron’s answer — if none of that works you can also try File > invalidate …
“Unresolved reference” in MainActivity.kt calling a TextView? [duplicate]
when I try to use a TextView in my activity_main.xml, I get a “Unresolved reference” in my MainActivity.kt (even before try building)? I just can’t see what I’m doing wrong!
I’ve highlighted the error at the end of the MainActivity.
MainActivity.kt
class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val carouselRecyclerview = findViewById(R.id.recycler) val list = ArrayList() list.add(DataModel(R.drawable.ad_astra, "Adastra")) list.add(DataModel(R.drawable.beach_bum, "Beach Bum")) list.add(DataModel(R.drawable.dark_phoenix, "Dark Phoenix")) list.add(DataModel(R.drawable.glass, "Glass")) val adapter = DataAdapter(list) carouselRecyclerview.adapter = adapter carouselRecyclerview.set3DItem(true) carouselRecyclerview.setAlpha(true) val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager() val currentlyCenterPosition = carouselRecyclerview.getSelectedPosition() carouselRecyclerview.setItemSelectListener(object : CarouselLayoutManager.OnSelected < override fun onItemSelected(position: Int) < //Cente item Toast.makeText(this@MainActivity, list[position].text, Toast.LENGTH_LONG).show() ShowMeIt.text = "Why does cause a Unresolved reference?" ShowMeIt.text = list[position].text >>) > >
activity_main.xml
I think you need to define on MainActivity.kt like this
val ShowMeIt= findViewById(R.id.ShowMeIt)
Java — Kotlin android studio holder.itemview issue, apply plugin: ‘kotlin-android-extensions’. and/or you are missing the import of your synthetic views at the top of this .kt file. When you are missing an import, you can put the cursor on the code that has the error, press Alt+Enter, and select the option to import the relevant class or function.
Why it’s unresolved reference error for calling setBackgroundResource() on an ‘ id ‘ in Mainactivity.kt?
package com.example.colormyviews import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.TextView class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setListeners() >private fun setListeners()< val boxOneText = findViewById(R.id.box_one_text) val boxTwoText = findViewById(R.id.box_two_text) val boxThreeText = findViewById(R.id.box_three_text) val boxFourText = findViewById(R.id.box_four_text) val boxFiveText = findViewById(R.id.box_five_text) val rootConstraintLayout = findViewById(R.id.info_text) val redButton = findViewById(R.id.red_button) val greenButton = findViewById(R.id.green_button) val yellowButton = findViewById(R.id.yellow_button) val clickableViews: List = listOf( boxOneText, boxTwoText, boxThreeText, boxFourText, boxFiveText, rootConstraintLayout, redButton, greenButton, yellowButton ) for (item in clickableViews) < item.setOnClickListener> > private fun makeColored(view: View) < when (view.id) < // Boxes using Color class colors for the background R.id.box_one_text ->view.setBackgroundColor(Color.DKGRAY) R.id.box_two_text -> view.setBackgroundColor(Color.GRAY) R.id.box_three_text -> view.setBackgroundColor(Color.BLUE) R.id.box_four_text -> view.setBackgroundColor(Color.MAGENTA) R.id.box_five_text -> view.setBackgroundColor(Color.BLUE) R.id.red_button -> box_three_text.setBackgroundResource(R.color.my_red) R.id.yellow_button -> box_four_text.setBackgroundResource(R.color.my_yellow) R.id.green_button -> box_five_text.setBackgroundResource(R.color.my_green) else -> view.setBackgroundColor(Color.LTGRAY) > > >
In makeColored() function, while calling setBackgroundResource() on box_three_text, box_four_text, box_five_text. It throws me an ‘ Unresolved reference error ‘. However id’s with same name has been declared in XML file. I can’t understand where the problem lies, Please help me to understand what’s going on and why it is unable to read id’s in Mainactivity.kt which has been declared in Mainactivity.xm.
instead of id use reference
R.id.red_button -> boxThreeText.setBackgroundResource(R.color.my_red) R.id.yellow_button -> boxFourText.setBackgroundResource(R.color.my_yellow) R.id.green_button -> boxFiveText.setBackgroundResource(R.color.my_green)
and make these buttons global variable. You can directly use ids if you are using kotlin synthetic, however I can’t see any such import in your code. (And anyway you should not be using be kotlin synthetic as Kotlin-extension plugin is deprecated)
Why it’s unresolved reference error for calling, R.id.red_button -> boxThreeText.setBackgroundResource(R.color.my_red) R.id.yellow_button -> boxFourText.setBackgroundResource(R.color.my_yellow) R.id.green_button -> boxFiveText.setBackgroundResource(R.color.my_green) and make these buttons …