【问题标题】:Why my recyclerView show Unresolved reference: recyclerView为什么我的 recyclerView 显示 Unresolved reference: recyclerView
【发布时间】:2023-01-15 12:57:31
【问题描述】:

我是 Android Studio 初学者,我想创建一个实用的回收视图。首先,我跟着这个视频学习了(https://www.youtube.com/watch?v=j29YnF-mPd8),但最后,我注意到一个“未解决的参考:recyclerView”错误。我该如何解决?

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
    }
}

以下是我的构建 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'

}

我的主要活动 XML 文件:

<?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>

感谢您提供解决方案来帮助我!

【问题讨论】:

    标签: android-studio kotlin android-recyclerview


    【解决方案1】:

    你必须在使用它之前声明它,只需添加:

    val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
    

    在 onCreate() 里面

    【讨论】:

      【解决方案2】:

      当你打电话时

      recyclerView.layoutManager=layoutManager //here got problem
              adapter=RecyclerViewAdapter()
              recyclerView.adapter=adapter //and here got problem
      

      您正在使用 recyclerView 变量,而没有先声明和初始化它。

      在通过这样的调用使用它之前对其进行初始化:

      val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
      recyclerView.layoutManager=layoutManager //here got problem
              adapter=RecyclerViewAdapter()
              recyclerView.adapter=adapter //and here got problem
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-08
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多