Eclipse - 以下类无法实例化: - android.support.v7.widget.RecyclerView

时间:2015-12-28 16:20:40

标签: java android eclipse initialization android-recyclerview

我正在尝试使用recycleler视图,但是eclipse给出了错误:...我的支持库是最新的:

这是错误:

enter image description here

这是我的布局,添加了recycler-view:。

// 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>

以下是将循环视图添加到活动的初始化视图代码:

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

这是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

我尝试了很多东西,例如添加回收器视图的支持项目和引用主项目等。 但没有一件事有效。

任何帮助将不胜感激:)。

1 个答案:

答案 0 :(得分:1)

在最新版本的支持库(22.x)中,CardView和RecyclerView库只提供了Android Studio(Gradle)集成,因此您需要手动创建Eclipse库项目才能在Eclipse中使用它们。 / p>

从Support v7库的v23.01开始,现在sdk中包含了库项目。

首先,确保您至少拥有支持库的v23.01:

enter image description here

然后,导航到sdk中支持库的位置:

enter image description here

recyclerview文件夹复制到您的项目中,我创建了一个名为&#34; dependency&#34;的文件夹。并将所有图书馆项目放在那里。

然后,将您的库项目与您的应用项目一起导入Eclipse:

enter image description here

对于每个图书馆项目,请确保选中Is Library

enter image description here

然后确保所有库项目都添加到您的应用项目中:

enter image description here

并确保所有库项目都包含在应用项目的构建路径中:

enter image description here

现在,您的Eclipse项目应该正确配置。

在这个简单的例子中,我使用了RecyclerView和CardViews。

这是片段代码:

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

}

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>

结果:

enter image description here