Chapter 7 Reference Error

The code you provided throws an “unresolved reference” error on line 28 of chapter 7.

28 listsRecyclerView = findViewById(R.id.lists_recyclerview)

“lists_recyclerview” throws an error in Android Studio 3.4.2

What needs to be changed in the code to allow this to compile?

Thanks!

Thanks for your comment. I am looping in the book team to get you an answer. Thanks! - Eric

Hi @andkot,

Welcome to the forums and sorry to hear you’re running into errors in the Chapter 7 sample code.

The error doesn’t appear for me in the final project using Android Studio 3.4.2 on macOS. That doesn’t mean there isn’t a problem though.

I’m wondering if there’s a set of steps you followed to get the error to show, also is your machine is running a different OS to mine?

If you can provide any of this information, as well as anything else relevant that will be helpful to continue figuring out what is happening.

Thanks,

Darryl

Hello Darryl,

Thanks for helping!

Ok so obvious differences first:
Win10
Gradle 5.1.1

The libraries are all different if you use gradle 5.1.1 so I think the problem may be here:


import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

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


When I run the final project with the version of gradle that you released it with, it compiles fine. Only when I use the code with newer libraries and gradle 5.1.1, the compiler throws the Unresolved reference error on “lists_recylerview”.

Thank you again.

Hey @andkot1,

Thank you for the extra information, it’s useful to know.

I opened the project again and updated it to use gradle 5.1.1 and the AndroidX libraries. This should be similar to the setup of the project on your machine.

The project compiles and the app appears in the emulator.

The next step we can try is looking at your project to see what could be different. Is it possible to upload your project somewhere for us to view?

Thank you,

Darryl

Here you be:

https://www.filedropper.com/listmakerandkot1

Again thank you so much for taking the time to help me on this, very frustrating after making very good progress to get stuck, and not understanding why.

If its worth anything, i dont think its a typo error, i can recreate this error by using your final project itself, then updating gradle, then it throws the error. But let me know…

THANKS!!

Brilliant!

Looking at the project, there’s a few things.

  1. In content_main.xml the RecyclerView doesn’t have an id. Without an id the app can’t find the recyclerView and throws an error. Fix this by adding the id to the RecyclerView in content_main.xml like so:

     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/lists_recyclerview" <--- Add this attribute
         android:layout_width="0dp"
         android:layout_height="0dp"
         app:layout_constraintBottom_toTopOf="@+id/textView"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent"/>
    
  2. The list_selection_view_holder.xml contains two TextViews, only the TextView with the id itemNumber is needed. The layout margin for the TextView is also set to 16, change this to 16dp. The dp lets the app know what measurement type to use to set up the TextView margins.

The TextView in list_selection_view_holder.xml will look like:

   <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/itemNumber"
        android:layout_margin="16dp"/> <--- Make sure the dp is added here

With the changes made, run the app. The errors will no longer appear.

I hope this helps you, let us know how you get on.

Thanks,

Darryl

Darryl,

  1. You da man.

  2. I cant possibly take accountability for this so I’m going to blame Hadi Hariri.

-eof-

Sounds like everything is ok now. :slight_smile:

We hope you enjoy the rest of the book!

Darryl