1

Using Android Studio 4.2.1 and trying to handle a RecyclerView I get an error when trying to build my own ViewAdapter.

I've added implementation 'androidx.recyclerview:recyclerview:1.2.1' to my build.gradle :app file.

My activity_main file is like this:

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

I've created another layout file like this:

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

I've created a Java class like this

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

The NotNull is Red and the "Problems" pane says "Cannot resolve symbol 'NotNull'". The public ViewHolder()... was auto-generated.

What, if anything, am I doing wrong? (This is not exactly homework. I'm not in an organized class. I am following on online tutorial that's about a year old and the instructor's AS is a slightly older version than mine.)

Zain
  • 37,492
  • 7
  • 60
  • 84
TecBrat
  • 3,643
  • 3
  • 28
  • 45

1 Answers1

1

You can discard it by removing the @org.jetbrains.annotations.NotNull annotation:

public ViewHolder(@NonNull View itemView) {

Or if you wish to leave it you need to include its dependency in build.gradle (Module level):

implementation 'org.jetbrains:annotations:16.0.2'
Zain
  • 37,492
  • 7
  • 60
  • 84
  • I tried removing that annotation, but had other errors later. I'll try it again and see if the other errors can be worked around. I'll also try adding the dependency you mentioned. Because I'm brand new at this, it just doubly confused me since the error is happening in auto-generated code. – TecBrat Jun 09 '21 at 00:24
  • I added the implementation to my gradle file and now it seems to be working. If I run into a similar issue, can you tell me how I'll find the correct implementation to add? Shouldn't AS have taken care of this automagically? – TecBrat Jun 09 '21 at 01:21
  • It depends, `implementation` reference a library, and libraries can have different source; for instance `@org.jetbrains.annotations.NotNull` didn't get resolved reference; so in this case we need to look at `jetbrains` to get this library implementation statement; This doesn't automatically imported on Android Studio as it's not a library from the default android libraries. – Zain Jun 09 '21 at 08:37
  • Another example when you try to use `RefreshLayout` in XML layout, AS offers you to auto import the `implementation` statement (probably you need to ALT+ENTER to import it); or it can be an external library like `Glide` so you need to go to their website/github But as a rule of thumb you need to go through documentation of the thing you need to import. – Zain Jun 09 '21 at 08:37