1

使用 Android Studio 4.2.1 并尝试处理RecyclerView我在尝试构建自己的ViewAdapter.

我已添加implementation 'androidx.recyclerview:recyclerview:1.2.1'到我的build.gradle:app 文件中。

我的 activity_main 文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:padding="10dp">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contactsRV"/>

</RelativeLayout>

我创建了另一个这样的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:id="@+id/parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact Name"
        android:id="@+id/txtName"/>

</RelativeLayout>

我已经创建了一个这样的 Java 类

package com.domain.packagename;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ContactRVAdapter {
    public class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(@NonNull @org.jetbrains.annotations.NotNull View itemView) {
            super(itemView);
        }
    }
}

是红色的NotNull,“问题”窗格显示“无法解析符号'NotNull'”。public ViewHolder()... 是自动生成的。

如果有的话,我做错了什么?(这不完全是家庭作业。我不在一个有组织的班级。我正在学习大约一年前的在线教程,而讲师的 AS 版本比我的稍旧。)

4

1 回答 1

1

您可以通过删除@org.jetbrains.annotations.NotNull注释来丢弃它:

public ViewHolder(@NonNull View itemView) {

或者如果你想离开它,你需要在 build.gradle (模块级别)中包含它的依赖项:

implementation 'org.jetbrains:annotations:16.0.2'
于 2021-06-08T22:26:37.490 回答