I am a Android Studio starter and I want to create a Recycle View for practical. First, I have followed this video to study ( https://www.youtube.com/watch?v=j29YnF-mPd8 ) but at the end, I noticed an "Unresolved reference: recyclerView" error. How I can solve it ?
package com.example.recycleview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
private var layoutManager: RecyclerView.LayoutManager?=null
private var adapter:RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
layoutManager=LinearLayoutManager(this)
recyclerView.layoutManager=layoutManager //here got problem
adapter=RecyclerViewAdapter()
recyclerView.adapter=adapter //and here got problem
}
}
Below are my build gradle :
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.recycleview"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs',includes: ['*.jar'])
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
// For control over item selection of both touch and mouse driven selection
implementation 'androidx.recyclerview:recyclerview-selection:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
My main activity XML file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thx for giving solution to help me !
2
Answers
You have to declare it before using it, just add:
inside onCreate()
When you are calling
You are using the recyclerView variable without having it declared and initialized first.
Initialize it before using it with a call like this: