Animesh Mangla
Animesh Mangla

Reputation: 773

Eclipse-The following classes could not be instantiated: - android.support.v7.widget.RecyclerView

I am trying to use recycler view but eclipse giving error:...my support libraries are up-to-date :

This is Error:

enter image description here

here is my layout that adding recycler-view :.

//part of xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
    xmlns:widget="http://schemas.android.com/apk/res-auto"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" 
    android:id="@+id/playeramainlayout"
    android:background="@color/black"> 

    <LinearLayout
        android:id="@+id/hf "
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:background="@color/black"
        android:gravity="center" >

      <!-- Player Buttons -->
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:background="@layout/rounded_corner"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <!-- Previous Button -->
            <ImageButton
                android:id="@+id/btnPrevious" 
                android:src="@drawable/btn_previous"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"/>
            <!-- Play Button -->

            <ImageButton
                android:id="@+id/btnPlay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"

                android:src="@drawable/btn_play" />


                android:background="@null"/>
            <!-- Next Button -->
            <ImageButton 
                android:id="@+id/btnNext"
                android:src="@drawable/btn_next"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"/>

        </LinearLayout>
    </LinearLayout>

    <!-- Progress Bar/Seek bar -->
    <!-- Timer Display -->

    <FrameLayout
        android:id="@+id/imageviewkalayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/player_header_bg"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/HSVfoalbumarinplayeractivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       </android.support.v7.widget.RecyclerView>   
        <ImageButton
            android:id="@+id/btnRepeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:background="@null"
            android:src="@drawable/btn_repeat" />

        <ImageButton
            android:id="@+id/btnShuffle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="65dp"
            android:background="@null"
            android:src="@drawable/btn_shuffle" />

        <ImageButton
            android:id="@+id/equaliser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:background="@null"
            android:src="@drawable/btn_repeat" />

    </FrameLayout>

    <LinearLayout
        android:id="@+id/timerDisplay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/songProgressBar"
        android:layout_centerHorizontal="true"
        android:background="@color/black" >

        <TextView
            android:id="@+id/songCurrentDurationLabel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left"
            android:background="@color/black"

            android:textColor="#eeeeee"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/songTotalDurationLabel"
            android:layout_width="155dp"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:gravity="right"
            android:textColor="#eeeeee"
            android:textStyle="bold" />
    </LinearLayout>

    <SeekBar
        android:id="@+id/songProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/  hf "
        android:layout_alignParentLeft="true"
        android:background="@color/black"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:progressDrawable="@drawable/seekbar_progress"
        android:thumb="@drawable/seek_handler" />

    <RelativeLayout
        android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:layout_above="@+id/timerDisplay"
        android:layout_alignParentLeft="true"
        android:background="@color/black"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" >

        <TextView
            android:id="@+id/songTitle"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:singleLine="true" />

        <TextView
            android:id="@+id/songartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:singleLine="true" />
    </RelativeLayout>

</RelativeLayout>

Here is the code oof initialising view that is adding recycler view to activity :

@Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.player);

        HSVforimageGallery=(RecyclerView) findViewById(R.id.HSVfoalbumarinplayeractivity);
        HSVforimageGallery.setLayoutManager(new LinearLayoutManager(this));
        HSVforimageGallery.setHasFixedSize(true);
}

Here is logcat :

12-28 21:40:32.055: E/AndroidRuntime(25525): FATAL EXCEPTION: main
12-28 21:40:32.055: E/AndroidRuntime(25525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView

12-28 21:40:32.055: E/AndroidRuntime(25525):    at android.app.Activity.setContentView(Activity.java:1881)
12-28 21:40:32.055: E/AndroidRuntime(25525):    at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:114)
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
12-28 21:40:32.055: E/AndroidRuntime(25525):    at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:481)
12-28 21:40:32.055: E/AndroidRuntime(25525):    at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:450)
12-28 21:40:32.055: E/AndroidRuntime(25525):    ... 26 more

I tried lots of things like adding support project of recycler-view and referencing to main project etc . But none of the thing worked .

Any help will be appreciated :).

Upvotes: 0

Views: 1226

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

In recent versions of the support library (22.x), the CardView and RecyclerView libraries were provided with only Android Studio (Gradle) integration, so you needed to manually create Eclipse library projects in order to use them in Eclipse.

As of v23.01 of the Support v7 library, however, there are now library projects included in the sdk.

First, make sure that you have at least v23.01 of the Support Library:

enter image description here

Then, navigate to the location of the Support Library in the sdk:

enter image description here

Copy the recyclerview folder to your project, I created a folder called "dependency" and placed all library projects in there.

Then, import your library project into Eclipse, along with your app project:

enter image description here

For each library project, ensure that Is Library is checked:

enter image description here

Then ensure that all library projects are added to your app project:

enter image description here

And, ensure that all library projects are included in the build path of your app project:

enter image description here

Now, your Eclipse project should be configured correctly.

In this simple example, I used a RecyclerView and CardViews.

Here is the Fragment code:

public class BlankFragment extends Fragment {


    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);
        MyAdapter adapter = new MyAdapter(new String[]{"testone", "testtwo", "testthree", "testfour"});
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }

}

The Adapter for the RecyclerView:

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;

    // 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 static class MyViewHolder extends RecyclerView.ViewHolder {
        public CardView mCardView;
        public TextView mTextView;
        public MyViewHolder(View v) {
            super(v);

            mCardView = (CardView) v.findViewById(R.id.card_view);
            mTextView = (TextView) v.findViewById(R.id.tv_text);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.card_item, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

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

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

fragment_blank.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.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
       </android.support.v7.widget.RecyclerView>   
</RelativeLayout>

card_item.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="48dp" >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView 
            android:id="@+id/tv_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" >       
            </TextView>

      </android.support.v7.widget.CardView>

</RelativeLayout>

Result:

enter image description here

Upvotes: 1

Related Questions