34

I tried to add RecyclerView and CardView into my project

dependencies {
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v13:21.0.0'
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.viewpagerindicator:library:2.4.1@aar'
compile project(':facebook')
}

it compiles, but I got below exception when run it on device

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RecyclerView" on path: DexPathList[[zip file "/data/app/xxxx.apk"],nativeLibraryDirectories=[/data/app-lib/xxxx, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at android.view.LayoutInflater.createView(LayoutInflater.java:559)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
Rene Xu
  • 1,093
  • 1
  • 9
  • 14

9 Answers9

65

Problem in your layout. Change

<RecyclerView 
   ...

To

<android.support.v7.widget.RecyclerView
   ...

If you create RecyclerView programmatically - make sure you have proper import:

import android.support.v7.widget.RecyclerView;
localhost
  • 5,568
  • 1
  • 33
  • 53
  • yeah, looks like I am using the latest buildtools and it do have the android.view.RecyclerView, so there is no error in android studio, but I am running it on a 4.4.4 device... – Rene Xu Oct 28 '14 at 09:31
  • In my layout, I have written same, still facing same problem. In CS class I am able to find android.support.v7.widget but not android.support.v7.widget.RecyclerView – Ajay Sharma Mar 22 '16 at 12:53
5

I did all what have been said in this post but nothing worked.

What did work for me:

1.Add this as say up in your build.gradle:

compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:cardview-v7:+'

2.Add the RecyclerView as a standar View and indicate the class:

    <view
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="android.support.v7.widget.RecyclerView"
    android:id="@+id/my_recycler_view"
    />

3.Then add the imports:

import android.support.v7.widget.RecyclerView;

Hope this helps!

khsoldier
  • 51
  • 1
  • 1
3

If you're looking for solution in 2019, you could try to change android.support.v7.widget.RecyclerView for androidx.recyclerview.widget.RecyclerView. It works for me. Hope it helps!

Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
2

you can use androidx recyclerview widget, this works !

 <androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:scrollbars="vertical"
    android:id="@+id/listExp"
    />

and import to code,

import androidx.recyclerview.widget.RecyclerView;

and then ready to use ,

 private RecyclerView recyclerViewExp;
Mucahid Uslu
  • 367
  • 3
  • 11
0

This worked for me:

compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:cardview-v7:+'
Dharman
  • 30,962
  • 25
  • 85
  • 135
lgallard
  • 462
  • 3
  • 10
  • By the way, rendering in Android Studio has some isssues http://code.google.com/p/android/issues/detail?id=72117 http://code.google.com/p/android/issues/detail?id=85118, in my case in version 1.1.0 a don't have previews for recyclerview – lgallard Apr 15 '15 at 15:06
0

in eclipse, you need to clear other(older) versions of android-support-v4.jar from other libraries which you import directly or indirectly.

for my instance ; my project(TurkRenkleri) includes MobilePlayGround which includes appcompat library(which has a older version of support v4 jar). So i didnt get the error given below, instead i got RecyclerView ClassNotFound.

after doing this , i finally got the error below, and understood problem therefore the solution(delete old jars).

[2015-04-29 00:59:53 - TurkRenkleri] Found 2 versions of android-support-v4.jar in the dependency list,
 [2015-04-29 00:59:53 - TurkRenkleri] but not all the versions are identical (check is based on SHA-1 only at this time).
 [2015-04-29 00:59:53 - TurkRenkleri] All versions of the libraries must be the same at this time.
 [2015-04-29 00:59:53 - TurkRenkleri] Versions found are:
 [2015-04-29 00:59:53 - TurkRenkleri] Path: E:\calisma alani\oyunlar\android-support-v7-appcompat\libs\android-support-v4.jar
 [2015-04-29 00:59:53 - TurkRenkleri]   Length: 621451
 [2015-04-29 00:59:53 - TurkRenkleri]   SHA-1: 5896b0a4e377ac4242eb2bc785220c1c4fc052f4
 [2015-04-29 00:59:53 - TurkRenkleri] Path: E:\calisma alani\oyunlar\RecyclerView\libs\android-support-v4.jar
 [2015-04-29 00:59:53 - TurkRenkleri]   Length: 1157388
 [2015-04-29 00:59:53 - TurkRenkleri]   SHA-1: 605c447c20ca216b5556af9f215af5d4bba1b117
 [2015-04-29 00:59:53 - TurkRenkleri] Jar mismatch! Fix your dependencies
Community
  • 1
  • 1
Alp
  • 1,863
  • 1
  • 20
  • 38
0

Make sure is compile 'com.android.support:recyclerview-v7:22.2.0'

Libin Thomas
  • 1,132
  • 13
  • 17
0

This worked for me. Add this to app gradle

compile 'com.android.support:design:23.1.1'

Then in your layout

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/recyclerView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

and in your activity

import android.support.v7.widget.RecyclerView;

and inside your onCreate

RecyclerView recyclerView;
F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36
-2

It is a Building problem...

Yes in few cases , The code was working previously and suddenly it stopped working (crash at app startup) when I synced and built an older version of the code.

The fix was to just close and restart Eclipse and clean the project and clean all the dependent library projects. Then it started working properly again.

It's some sort of build problem in Eclipse, when refreshing the project files.

Update: In particular, if you've accidentally modified the ".classpath" file (to revert to an older version), Eclipse/Android SDK can get confused and not build the project properly. When you restart Eclipse and clean the project, Eclipse will re-modify the ".classpath" file, and build properly.

Sainath Patwary karnate
  • 3,165
  • 1
  • 16
  • 18
  • If you are going to provide an answer from another related question, please link http://stackoverflow.com/a/20421764 instead of copy+paste. – tir38 Jun 11 '15 at 22:04