Failed to instantiate RecyclerView


#1

UNFORTUNATELY, I am having problem with the RecyclerView class. After adding the RecyclerView class in the dependencies I created the fragment_crime_list.xml file and since then I am getting the error :-

Failed to instantiate one or more classes :
The following class could not be instantiated :-
-android.support.v7.widget.RecyclerView(Open class,Show Exception)

and the exception details are :

java.lang.ClassNotFoundException: android.view.View$OnUnhandledKeyEventListener
	at org.jetbrains.android.uipreview.ModuleClassLoader.load(ModuleClassLoader.java:180)
	at com.android.tools.idea.rendering.RenderClassLoader.findClass(RenderClassLoader.java:61)
	at org.jetbrains.android.uipreview.ModuleClassLoader.findClass(ModuleClassLoader.java:118)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at org.jetbrains.android.uipreview.ModuleClassLoader.loadClass(ModuleClassLoader.java:213)
	at android.support.v7.widget.RecyclerView.initAutofill(RecyclerView.java:702)
	at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:629)
	at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:601)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
	at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
	at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
	at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:209)
	at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
	at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
	at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
	at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
	at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
	at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
	at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
	at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
	at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:544)
	at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:678)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

I have already tried all other methods given by other people but non of them seem to work.

  • I rebuilt the project a number of times but them didn’t solve the problem.
  • I tried to change the SDK versions in the build.gradle file “as suggested by many other people on this forum”
  • I also tried to ‘Clean Project’ and ‘Invalidate Caches/Restart’ again “as suggested by many other people on this forum”

and a few other ways suggested by people on the internet.

When I wasn’t able to solve the problem then I updated everything and now I am presently working with Android 3.1.3 and also with an upgraded Gradle, but non of it seems to solve the problem.

Any kind of help from anyone will be really nice
PLEASE HELP :anguished::anguished::anguished:


#2

Please share your build.gradle under folder app for further investigation.


#3

I am providing the build.gradle file from Module:app as you have asked :smiley:

apply plugin: 'com.android.application'

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

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
    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'
    implementation 'com.android.support:recyclerview-v7:28.0.0-alpha3'
}

#4

I don’t find anything strange in this file.
Could you please share the fragment_crime_list.xml as well?

A case with the similar back trace to yours.


#5

This is my fragment_crime_list.xml file as asked :-

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/crime_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

#6

I would also like to state that the app works just fine when I run it on my device, it’s only the Android Studio in which I come across the error.

Here is a screenshot of fragment_crime_list.xml from the Android Studio window :-


#7

Please try Switching the preview’s API level to lower one from the preview configuration as the answer in stackoverflow.


#8

As suggested, I also tried to lower down the API version in the editor but it didn’t work.