9365


`RecyclerView`と`CardView`を使ってリストを作ったいるのですが､`Adapter`内の`onCreateViewHolder`でアイテム用の`CardView`をinflateするときに以下のエラーが出てしまいます｡

[code]

    android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    
[/code]

`LayoutInflater`が取得出来ていないかと考えたのですが､取得出来ていましたし､アイテムに入れるべきデータもしっかりと取得出来ていました｡  
`Adapter`は以下のようになっていて､Activityの内部クラスとして定義されています｡

[code]

    private class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder>{
    
            private LayoutInflater mInflater;
            private List<Video> mVideoInfo;
            private Context mContext;
            private int mResourceId;
    
            public SearchAdapter(Context context, int resourceId){
                super();
                mContext=context;
                mResourceId=resourceId;
                mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mVideoInfo = new ArrayList<>();
            }
    
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
                View v = mInflater.inflate(mResourceId, parent, false);
                return new ViewHolder(v);
            }
    
            @Override
            public int getItemCount(){
                return (null != mVideoInfo ? mVideoInfo.size() : 0);
            }
    
            @Override
            public void onBindViewHolder(ViewHolder holder, int position){
                Video item = mVideoInfo.get(position);
                holder.title.setText(item.title);
                holder.duration.setText(item.duration);
                Picasso.with(getApplicationContext())
                        .load(item.thumbnails.medium.url)
                        .placeholder(R.drawable.thumbnail_background_480)
                        .transform(new ViewUtils.YouTubeThumbnailEditor())
                        .into(holder.thumbnail);
            }
    
    
            class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, PopupMenu.OnMenuItemClickListener{
                TextView title;
                ImageView thumbnail;
                TextView companyAndGoods;
                TextView duration;
                ImageView optionButton;
                RelativeLayout item;
    
                public ViewHolder(View v){
                    super(v);
                    title = findById(v, R.id.title);
                    thumbnail = findById(v, R.id.thumbnail);
                    companyAndGoods = findById(v, R.id.company_and_goods);
                    duration = findById(v, R.id.duration);
                    optionButton = findById(v, R.id.option_button);
                    item = findById(v, R.id.item);
                    optionButton.setTag(OPTION + mVideoInfo.get(getAdapterPosition()).id);
                    optionButton.setOnClickListener(this);
                    item.setTag(VIDEO+mVideoInfo.get(getAdapterPosition()).id);
                    item.setOnClickListener(this);
                }
    
                @Override
                public void onClick(View v) {
                    String tag = (String)v.getTag();
                    if(tag.substring(0, VIDEO.length()).equals(VIDEO)){
                        moveToVideoActivity(tag.substring(VIDEO.length()));
                    }else if(tag.substring(0, OPTION.length()).equals(OPTION)){
                        PopupMenu popupMenu = new PopupMenu(SearchActivity.this, v);
                        popupMenu.getMenuInflater().inflate(R.menu.video_option_menu, popupMenu.getMenu());
                        popupMenu.setOnMenuItemClickListener(this);
                        popupMenu.show();
                    }
                }
    
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    //TODO : お気に入り処理
                    return false;
                }
            }
            public void addAll(List<Video> object) {
                mVideoInfo.addAll(object);
                notifyDataSetChanged();
            }
    
    
        }
    
[/code]

次の`onCreate()`の箇所で`RecyclerView`を初期化しています｡

[code]

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mAdapter = new SearchAdapter(this, R.layout.search_item);
            mRecyclerView.setAdapter(mAdapter);
    
[/code]

また`RecyclerView`とアイテム用の`CardView`は以下のようになっています｡

**RecyvlerView**

[code]

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:background="@color/background"/>
    
[/code]

**CardView**

[code]

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        app:cardCornerRadius="?attr/cardCornerRadius"
        android:clickable="true">
    
        <com.balysv.materialripple.MaterialRippleLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:mrl_rippleColor="@android:color/darker_gray">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:id="@id/thumbnail"
                    android:layout_width="480px"
                    android:layout_height="270px"
                    android:contentDescription="@string/sample_image"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"/>
    
    
                <ImageView
                    android:id="@+id/option_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_action_overflow"
                    android:contentDescription="@string/sample_image"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"/>
                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:maxLines="2"
                    android:layout_toRightOf="@id/thumbnail"
                    android:layout_toEndOf="@id/thumbnail"
                    android:layout_alignParentTop="true"
                    android:layout_toStartOf="@id/option_button"
                    android:layout_toLeftOf="@id/option_button"/>
    
                <TextView
                    android:id="@+id/duration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"/>
    
                <TextView
                    android:id="@+id/company_and_goods"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:layout_toRightOf="@id/thumbnail"
                    android:layout_toEndOf="@id/thumbnail"
                    android:layout_alignParentBottom="true"
                    android:layout_toStartOf="@id/duration"
                    android:layout_toLeftOf="@id/duration"/>
    
            </RelativeLayout>
        </com.balysv.materialripple.MaterialRippleLayout>
    
    </android.support.v7.widget.CardView>
    
[/code]

何度もデバッグしてみたのですが､原因となる箇所を特定できませんでした｡ なにかこのエラーを回避する方法がありましたら教えていただけるとありがたいです｡
よろしくおねがいします｡


CC BY-SA 3.0
2015-04-21T00:30:54.787
9361
2015-04-21T04:12:49.127
2015-04-21T03:27:42.393
7232
7232
question
android android-layout
android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>


これを

[code]

    <android.support.v7.widget.CardView
        ・・・省略
        app:cardCornerRadius="?attr/cardCornerRadius"
        ・・・省略
    
[/code]

こうすればいいと思います。

[code]

    <android.support.v7.widget.CardView
        ・・・省略
        app:cardCornerRadius="2dp" <!-- もしくはこの行を削除 -->
        ・・・省略
    
[/code]


CC BY-SA 3.0
2015-04-21T04:12:49.127
9365
2015-04-21T04:12:49.127
8216
9361
answer