16

I'm getting this exception when running:

android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: /data/app/my.package.location-1.apk

There are some questions concerning this error, from which I've learned to:
- specify exactly the support.v7 RecyclerView, in the xml and Java code.
- in Eclipse, I added this jar file as a library to the project:
adt-bundle-windows-x86_64-20140321\sdk\extras\android\support\v7\recyclerview\libs\android-support-v7-recyclerview.jar
- in Eclipse, import the existing project TestActivity in *adt-bundle-windows-x86_64-20140321\sdk\extras\android\support\v7\recyclerview* and then added that project to the Java Build Path of my own project.

Project Build Target is Android 5.1.1/API 22

All to no effect. What else is there?

from MyFragment.java

import android.support.v7.widget.RecyclerView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
            final Activity thisActivity = getActivity();
            final RecyclerView recyclerView = (RecyclerView)thisActivity.findViewById(R.id.my_listview);

            final List<String> list = Arrays.asList(HEADERS);

            final MyRecyclerAdapter adapter = new MyRecyclerAdapter(list);
            recyclerView.setAdapter(adapter);

}

MyRecyclerAdapter.java

import android.support.v7.widget.RecyclerView;

    public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
      private ArrayList<String> mDataset;

      // Provide a suitable constructor (depends on the kind of dataset)
      public MyRecyclerAdapter(List<String> list) {
        mDataset = (ArrayList<String>) list;
      }

      // Create new views (invoked by the layout manager)
      @Override
      public MyRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

          // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
      }

      // Replace the contents of a view (invoked by the layout manager)
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        final String name = mDataset.get(position);
        holder.txtId.setText(mDataset.get(position));
        holder.txtId.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                  remove(name);
                }
            });

        holder.txtType.setText("Footer: " + mDataset.get(position));

      }

      // Return the size of your dataset (invoked by the layout manager)
      @Override
      public int getItemCount() {
        return mDataset.size();
      }
      // Provide a reference to the views for each data item
      // Complex data items may need more than one view per item, and
      // you provide access to all the views for a data item in a view holder

      public class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView txtId;
        public TextView txtType;
        public TextView txtName;

        public ViewHolder(View v) {
          super(v);
          txtId = (TextView) v.findViewById(R.id.id);
          txtType = (TextView) v.findViewById(R.id.type);
          txtName = (TextView) v.findViewById(R.id.name); 
         }
      }

      public void add(int position, String item) {
        mDataset.add(position, item);
        notifyItemInserted(position);
      }

      public void remove(String item) {
        int position = mDataset.indexOf(item);
        mDataset.remove(position);
        notifyItemRemoved(position);
      }

    } 

fragment.xml

<?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="match_parent"
    android:orientation="horizontal"
    android:background="#000000" >

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

</RelativeLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/id"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/type"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/name"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" >
    </TextView>

</LinearLayout> 

Edit: As suggested by Arman Kabir, I checked "Is Library". This does indeed fix the ClassNotFoundException. It does result in a slightly different error, but this is another problem.

android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView
    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
    ... 44 more
Caused by: java.lang.NoClassDefFoundError: android.support.v4.util.Pools$SimplePool
    at android.support.v7.widget.AdapterHelper.<init>(AdapterHelper.java:56)
    at android.support.v7.widget.AdapterHelper.<init>(AdapterHelper.java:71)
    at android.support.v7.widget.RecyclerView.initAdapterManager(RecyclerView.java:455)
    at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:339)
    ... 47 more
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139

7 Answers7

53

If you are updated your android studio to v-3.4.2

then Change from

android.support.v7.widget.RecyclerView

to

androidx.recyclerview.widget.RecyclerView

it's work for me.

Sujeet Kumar
  • 1,822
  • 22
  • 25
8

In eclipse in your workspace, create a new project using the existing code, then select path to Recycler in android SDK support and in properties select compiler google API 20 or 21 and check Is Library.

After that, in the workspace select your own project, right-click properties and go to android section and in library click add button and select your Recycler project from list.

Next you must do clean all project from project menu .

Sorry if my english is so bad but its ur solution and just adding suuport v7 as jar through errors its not like v4.

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Arman Kabir
  • 126
  • 5
8
Simple Two Steps:

In stand off

<android.support.v7.widget.RecyclerView
            android:id="@+id/rv_News"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />  

Replace with androidx package in xml file as below

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

@Ambilpura
5

Switching to Android Studio is a better option but the rendering problem is still there.

To remove the same in Android Studio, add the "Gradle Dependencies" into your module.

 dependencies {
    ...
    compile 'com.android.support:recyclerview-v7:21.0.+'
   }

Hope this helps somebody as it helped me. This is a small change but very annoying.

duplode
  • 33,731
  • 7
  • 79
  • 150
Lazycoder-007
  • 1,055
  • 9
  • 19
1

If you do not know which jar file contains the class your are looking for you can use this bash command. On Windows, you can run it if you install Cygwin.

for i in find . -name \*.jar ; do echo $i; jar tvf $i | fgrep $*; done

This should search in the subdirectories and help you find the missing jar file.

1

This is my eclipse journey to complete my mqtt project.

First of all, you have to check if the support library exists, if not you have to download it using sdk manager.

enter image description here

I know you've already got that library. Just check the directory where the support library is. You are sure to confirm there are four directories existed on the picture.

enter image description here

In your case, you will have two core libraries both appcompat-v7 and support-v4 that are all version 22 or higher at least.

There must be a aar file in each folders. you have to change the suffix aar to zip, then unzip it under the folder. Rename both classes.jar to appcompat-v7-22.1.0.jar and support-v4-22.1.0.jar.

It means that you are ready to copy them to the proper libs folder in your project.

enter image description here

you do the same action with both folders.

enter image description here

In the recyclerview-v7\21.0.0 folder, you can juse rename the classes.jar to whatever name you want and the support-annotations-21.0.0.jar under the support-annotations\21.0.0\ then copy them to the your libs folder of your project.

Import existing android projects to your eclipse,

enter image description here

Also, do the same thing with the support-v4-22.1.0 library.

Don't forget check the checkbox - 'Is Library'

You can finally set up all configuration for your project like picture as follows.

enter image description here

This is my project.properties file.

target=android-21

java.target=1.7
java.source=1.7

android.library.reference.1=..\\MqttService
android.library.reference.2=..\\extras\\android\\m2repository\\com\\android\\support\\appcompat-v7\\22.1.0\\appcompat-v7-22.1.0
android.library.reference.3=..\\extras\\android\\m2repository\\com\\android\\support\\support-v4\\22.1.0\\support-v4-22.1.0
tommybee
  • 2,409
  • 1
  • 20
  • 23
0

implementation 'com.google.android.material:material:1.4.0-alpha02'

If this has in your dependency list, You should remove this and any other recycle-view related dependencies and sync the Gradle.

and add these two dependencies.

implementation 'com.android.support:appcompat-v7:21.0.3'

implementation 'com.android.support:recyclerview-v7:21.0.0'