0

Why do I keep getting "Unresolved reference:layout" message in this part of my code (i have been creating a custom adapter for a RecyclerView item, the item itself is contained within the game.xml file):

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GameViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.game, parent, false)
    return GameViewHolder(view)
}

To be more specific, the "layout" in R.layout.game is red in color(not underlined) and when i hover over it i get a previously mentioned message.

Same happens for "id" keyword in this part:

inner class GameViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 
    val gameWinner: TextView = itemView.findViewById(Build.VERSION_CODES.R.id.winner_textview)
    val highscore: TextView = itemView.findViewById(Build.VERSION_CODES.R.id.highscore)
}

I have been stuck on this for a while, any help is welcomed. Thanks in advance!

I though this had something to do with Gradle, since i had issues with it before, but even after it synced successfully, the same issue persists.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154

1 Answers1

1

Unresolved reference:layout

This means what R-class know nothing about "layout" member. Do you really have any folder called "layout" in yours res folder? You can check it at project structure window (Alt+1 by default) or just type "R." in android studio and checking what it will propose.

Same happens for "id" keyword in this part:

Build.VERSION_CODES.R and R.id.winner_textview are two separate things.

First one is a constant Int value for API version code R which is Android 11 (more about it: android dev site and here)

Second one is yours R-class which refer for id-class which holds all defined id's from your ".xml" files. So i guess you want something like what:

val gameWinner: TextView = itemView.findViewById(R.id.winner_textview)
val highscore: TextView = itemView.findViewById(R.id.highscore) 

Read more about R-class here.

MrCatDog
  • 23
  • 5