找不到类"android.support.v7.widget.RecyclerView" [英] Didn't find class "android.support.v7.widget.RecyclerView

查看:479
本文介绍了找不到类"android.support.v7.widget.RecyclerView"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时出现此异常:

android.view.InflateException:二进制XML文件第8行:错误膨胀了类android.support.v7.widget.RecyclerView
原因:java.lang.ClassNotFoundException: 在以下路径中找不到类"android.support.v7.widget.RecyclerView":/data/app/my.package.location-1.apk

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

关于此错误,有一些问题,从中学到:
-在xml和Java代码中准确指定support.v7 RecyclerView.
-在Eclipse中,我将此jar文件作为库添加到了项目中:
adt-bundle-windows-x86_64-20140321 \ sdk \ extras \ android \ support \ v7 \ recyclerview \ libs \ android-support-v7-recyclerview.jar
-在Eclipse中,将现有项目TestActivity导入* adt-bundle-windows-x86_64-20140321 \ sdk \ extras \ android \ support \ v7 \ recyclerview *中 然后将该项目添加到我自己项目的Java Build Path中.

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.

项目构建目标是Android 5.1.1/API 22

Project Build Target is Android 5.1.1/API 22

全部无效. 还有什么?

All to no effect. What else is there?

来自MyFragment.java

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

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

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

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> 

根据Arman Kabir的建议,我选中了是图书馆".这确实可以修复ClassNotFoundException.确实会导致稍有不同的错误,但这是另一个问题.

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

推荐答案

在工作区中的eclipse中,使用现有代码创建一个新项目,然后在android SDK支持中选择Recycler的路径,并在属性中选择编译器Google API 20或21,然后选择是图书馆".

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.

然后,在工作区中选择您自己的项目,右键单击属性,然后转到android部分,然后在库中单击添加"按钮,然后从列表中选择Recycler项目.

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 .

对不起,如果我的英语不好,但是它是您的解决方案,只是通过错误将suuport v7添加为jar而不是v4.

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

这篇关于找不到类"android.support.v7.widget.RecyclerView"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆