ghostrider
ghostrider

Reputation: 2238

Error inflating class Recycler View

I am a beginner in Android and trying to learn the Recycler View using this simple example.But I am getting the exception as seen in the Logcat.I have included all the necessary support libraries and added them to the build path of the project. Following are my files related to the recycler view:

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);
}
}

Can someone please help.

Upvotes: 1

Views: 1109

Answers (1)

Hitesh Sahu
Hitesh Sahu

Reputation: 45160

If you are using eclipse you need to folow this steps

  1. Import app everything from C:\Productivity\android-sdks\extras\android\support\v7 . Very Important "Make sure you have checked copy into workspace option in import dialog box".

  2. Change SDK build version for all imported libraries to Android 6.0 from project properties , proceed with clean and build.

  3. Add Recyclerview and appcompat in your project build as library as shwon in pic below adding recycler view to build path

  4. Done now you can develop for material in eclipse.

Upvotes: 1

Related Questions