'Android Studio - Why might requireViewById(R.id.recyclerview) not be working in API 22 (it is in 28)

In short, API 28 all is well; API 22 app crashes in MainActivity.onCreate with

java.lang.NoSuchMethodError: No virtual method requireViewById

on the following line:

RecyclerView recyclerView = requireViewById(R.id.recyclerview);

Now, the long explanation:

I am setting up an Android Studio app. I have one MVVM "set" including a RecyclerView (within an android.support.design.widget.CoordinatorLayout) in my activity_main.xml; the android.support.v7.widget.RecyclerView has a tools:listitem="@layout/parcel_item"; the parcel_item.xml is an android.support.v7.widget.CardView.

Everything seems fine under API 28. When I run to API 22 - either on my physical Samsung Galaxy Note tablet (Android 5.1.1 API 22), or on a virtual Nexus 7 (2012) API 22 - the app is crashing almost immediately. In MainActivity.onCreate:

RecyclerView recyclerView = requireViewById(R.id.recyclerview);

with:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.zazzem.thatsthat, PID: 5613
    java.lang.NoSuchMethodError: No virtual method requireViewById(I)Landroid/view/View; in class Lcom/zazzem/thatsthat/MainActivity; or its super classes (declaration of 'com.zazzem.thatsthat.MainActivity' appears in /data/app/com.zazzem.thatsthat-1/split_lib_slice_7_apk.apk)
        at com.zazzem.thatsthat.MainActivity.onCreate(MainActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

The problem, No virtual method requireViewById seems to imply a virtual method that wasn't overridden somewhere, but - as I mentioned before - this works fine under API 28.

I've found nothing online specific to "No virtual method requireViewById" and I've tried everything suggested by a variety of posts relating to "java.lang.NoSuchMethodError" - including File | Invalidate Caches/Restart and checking my Gradle scripts (as well as I'm able; I'm very new to Android Studio & java). All to no avail.

MainActivity.java

//MainActivity.java
package com.zazzem.thatsthat;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ParcelViewModel parcelViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("##","before"); //this I get <<====================
        RecyclerView recyclerView = requireViewById(R.id.recyclerview);
        Log.d("##","after"); //this I don't <<====================
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final ParcelAdapter adapter = new ParcelAdapter();
        recyclerView.setAdapter(adapter);

        parcelViewModel = ViewModelProviders.of(this).get(ParcelViewModel.class);
        parcelViewModel.getAllParcels().observe(this, new Observer<List<Parcel>>() {
            @Override
            public void onChanged(@Nullable List<Parcel> parcels) {
                adapter.setParcels(parcels);
            }
        });
    }

    public void menuButtonClick(View view) {
        Toast.makeText(this, "menuButtonClick", Toast.LENGTH_SHORT).show();
    }
} //MainActivity

Help | Check for Updates... all current

Versions and such:

Android Studio 3.3.2

SDK Platforms:

-Android 9.0 (Pie) 28 (Revision 6) Installed -Android 8.1 (Oreo) 27 (Revision 3) Partially installed -Android 5.1 (Lollipop) 22 (Revision 2) Partially installed

SDK Tools:

-Android SDK Build-Tools 29-rc2 -Android Emulator b28.0.25 -Android SDK Platform-Tools v28.0.2 -Android SDK Tools v26.1.1 -Support Repository (all 4 installed: -,-,47.0.0,58)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/facadeLight"
        tools:listitem="@layout/parcel_item" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_menu"
        ...
        android:tint="@color/facadeLight" />

</android.support.design.widget.CoordinatorLayout>

parcel_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="24dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="24dp">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/parcel_theme">

        <TextView
            android:id="@+id/textview_Descr"
            ...
            android:textSize="60sp" />
        <TextView
            android:id="@+id/textview_ParcelID"
            ...
            android:textSize="12sp" />
        <TextView
            android:id="@+id/textview_NrPuzzles"
            ...
            android:textSize="20sp" />
        <TextView
            android:id="@+id/textview_AdCopy"
            ...
            android:text="Ad Copy" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

finally, the Gradle Scripts:

build.gradle (Project: ThatsThat)

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.zazzem.thatsthat"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //Lifecycle components
    implementation 'android.arch.lifecycle:extensions:1.1.1'
    annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

    //Room components
    implementation 'android.arch.persistence.room:runtime:1.1.1'
    annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
}


Solution 1:[1]

RecyclerView recyclerView = findViewById(R.id.recyclerview);

You have to update above line.

Solution 2:[2]

It's simple, requireViewById was added in API level 28, so the error message:

java.lang.NoSuchMethodError: No virtual method requireViewById

should be read literally -- on runtimes running API level less than 28, this method simply does not exist. Normally, this type of thing would be a compile error, however in this case the error is not raised at compile time, because you are compiling against a library that does have that method. However, the method is not present in runtime versions of the Android API < 28.

From: https://developer.android.com/reference/android/view/View#requireViewById(int):

Snippet from Reference Docs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mehul Kabaria
Solution 2