Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Didn't find class "android.support.v7.widget.RecyclerView

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
like image 234
Al Lelopath Avatar asked Apr 17 '15 21:04

Al Lelopath


2 Answers

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.

like image 131
Sujeet Kumar Avatar answered Oct 23 '22 15:10

Sujeet Kumar


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.

like image 29
Arman Kabir Avatar answered Oct 23 '22 15:10

Arman Kabir