扩充类Recycler View时出错

时间:2015-10-27 18:35:06

标签: android android-layout android-activity android-recyclerview

我是Android的初学者,并尝试使用这个简单的例子学习Recycler View。但是我得到了Logcat中的异常。我已经包含了所有必要的支持库,并将它们添加到了构建路径中。项目。 以下是与回收者视图相关的文件:

logcat的:

 Could not find class android.support.v7.widget.RecyclerView', referenced   from method           com.example.rtest.MainActivity.onCreate
10-27 23:36:14.480: E/AndroidRuntime(12806): FATAL EXCEPTION: main
10-27 23:36:14.480: E/AndroidRuntime(12806): java.lang.RuntimeException:   Unable to start activity ComponentInfo    {com.example.rtest/com.example.rtest.MainActivity}:  android.view.InflateException: Binary XML file line #7: Error inflating class     android.support.v7.widget.RecyclerView

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

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

>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

my_text_view.xml

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="Hello List view!!!"        
/>
</LinearLayout>

AdapterTest.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class AdapterTest extendsRecyclerView.Adapter<AdapterTest.ViewHolder>        {
private int[] mDataset;

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView)v;
    }
}


public AdapterTest(int [] myDataset) {
mDataset = myDataset;
}


@Override
public AdapterTest.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {

View v = LayoutInflater.from(parent.getContext()                           .inflate(R.layout.my_text_view, parent, false);

AdapterTest.ViewHolder vh = new AdapterTest.ViewHolder(v);
return vh;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);

}

@Override
public int getItemCount() {
return mDataset.length;
}
}

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends Activity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRecyclerView = (RecyclerView) findViewById(R.id.drawer);

int[] a = {1,2,3};

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new AdapterTest(a);
mRecyclerView.setAdapter(mAdapter);
}
}

有人可以帮忙。

1 个答案:

答案 0 :(得分:1)

如果您正在使用eclipse,则需要执行以下步骤

  1. 从C:\ Productivity \ android-sdks \ extras \ android \ support \ v7导入应用程序。非常重要“确保您已在导入对话框中选中了复制到工作区选项”

  2. 从项目属性中将所有导入库的SDK构建版本更改为Android 6.0,继续进行清理和构建。

  3. 将您的项目构建中的Recyclerview和appcompat添加为库中的shwon adding recycler view to build path

  4. 现在你可以在日食中开发材料了。