0

I'm new to kotlin, I Have an adapter class and I need to inflate a view. but recently I'm facing an error saying "Unresolved reference: R"

Like in the image below :

enter image description here

So, how can I inflate this view? I have imported the following:

import kotlinx.android.synthetic.main.slide_layout.view.*

But I can't figure out what should i do next, I searched everywhere, but they all seem outdated! so what should i do? Thank you!

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34

4 Answers4

1

There are two possible issues i can think of,

  1. The first is a missing import to the R class, if your adapter is in an inner package from your domain package name, i.e.
    • assume this is the domain package 'your.doamin.package'
    • if adapter is not in that package i.e. adapter is in 'your.domain.package.adapter' then you need to import the R class into that file, i.e write this import statement

import your.domain.package.R

That should fix your problem for this scenario

  1. If you have already done '1' above then the other issue is, Sometimes Android studio just misbehaves, so close the file and clean and build your project by first clicking, Build -> Clean Project, once the build is done reopen that file.
Ebi Igweze
  • 460
  • 3
  • 8
  • still having the same problem even after many rebuilds, screenshot: http://prntscr.com/ke6brs – Tamim Attafi Aug 03 '18 at 09:17
  • @TamimProduction try this, since you have the import, run the application in an emulator or device, and tell me if it crashes because of the R class, lets eliminate some possibilitiies – Ebi Igweze Aug 03 '18 at 09:35
  • @TamimProduction try this, since you have the import, first do this clean the build by going to menu: Build -> Clean Project, then build and run the application in an emulator or device, and tell me if it crashes because of the R class, lets eliminate some possibilitiies, – Ebi Igweze Aug 03 '18 at 09:42
  • it doesn't even build :/ – Tamim Attafi Aug 03 '18 at 09:52
  • Yh its not supposed to build sorry, then try this, it is possibly something else that is causing the R class madness, and it is possible that you have a bug somewhere else that is preventing the app from building, to figure that out, comment out this part of your code that uses the R class, and try and build your project, and make sure that it builds: fix any other missing imports and make sure it builds without the R class, once it builds successfully, uncomment that R class part, and rebuild. – Ebi Igweze Aug 03 '18 at 10:06
  • more errors happening , i think i'm gonna start a new project and see if it's still occuring – Tamim Attafi Aug 03 '18 at 10:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177332/discussion-between-ebi-igweze-and-tamim-production). – Ebi Igweze Aug 03 '18 at 10:23
0

For resolving the error unresolved reference: R, you are missing import of your R file. Something like

import packageName.R

For your reference, I am attaching a screenshot of the error and see the commented import. enter image description here So check where your R.java resides and import that.

MXC
  • 458
  • 1
  • 5
  • 21
0

First you check your class import statement in that your R class file imported or not.

import packageName.R

also you can set android studio provide auto import features. if you can enable this feature then all required class automatically import your .kt class

0

Unfortunately not all answers here can solve your problem, I've been looking for general solutions, you can find them Here

Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34