Recycler视图适配器InflateException

时间:2017-09-09 21:24:20

标签: android xml android-recyclerview adapter

当我在我的适配器“onCreateViewHolder”中给卡充气时,由于“InflateException”,我收到来自Google Play开发者控制台的崩溃报告,但我似乎无法找到问题的根源。以下是开发控制台的堆栈跟踪:

stack trace

以下是导致崩溃的方法:

@Override
    public HeatmapDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //there was a crash report (InflateException) while inflating this view, but I can't seem to find the issue?
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_heatmap, parent, false);
        HeatmapDataViewHolder heatmapDataViewHolder = new HeatmapDataViewHolder(listener, v);

        return heatmapDataViewHolder;
    }

我已阅读here,问题可能出在android:foreground="?android:attr/selectableItemBackground",但我找不到任何理由说明这就是问题所在。即使这是问题,我如何在卡片视图上获得涟漪效应?

这是我想要膨胀的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    card_view:cardCornerRadius="2dp"
    android:background="@color/light_gray"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:weightSum="4">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="3"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal">

                <com.makeramen.roundedimageview.RoundedImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/card_image_view"
                    android:layout_gravity="center"
                    android:scaleType="centerCrop"
                    app:riv_corner_radius="2dp"
                    android:src="@drawable/test_bkg"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|bottom"
                    android:background="@android:drawable/screen_background_dark_transparent"
                    android:orientation="vertical"
                    android:paddingLeft="14dp"
                    android:paddingRight="12dp"
                    android:paddingTop="8dp"
                    android:paddingBottom="8dp">

                    <TextView
                        android:id="@+id/title_text_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"
                        android:textColor="@color/white"
                        android:textStyle="bold"
                        android:text="Title"/>

                    <TextView
                        android:id="@+id/subtitle_text_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="12dp"
                        android:textColor="@color/white"
                        android:textStyle="bold"
                        android:text="Sub Title"/>

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center|right"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <ImageView
                android:id="@+id/delete_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/icon_padding"
                android:src="@drawable/ic_delete_black_24dp"
                android:tint="@color/dark_gray"
                android:background="?android:attr/selectableItemBackgroundBorderless"    />

            <ImageView
                android:id="@+id/edit_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/icon_padding"
                android:src="@drawable/ic_edit_black_24dp"
                android:tint="@color/dark_gray"
                android:background="?android:attr/selectableItemBackgroundBorderless"    />

            <ImageView
                android:id="@+id/share_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/icon_padding"
                android:src="@drawable/ic_share_black_24dp"
                android:tint="@color/dark_gray"
                android:background="?android:attr/selectableItemBackgroundBorderless"    />

            <ImageView
                android:id="@+id/view_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/icon_padding"
                android:src="@drawable/ic_view_24dp"
                android:tint="@color/dark_gray"
                android:background="?android:attr/selectableItemBackgroundBorderless"    />

        </LinearLayout>

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

提前致谢。

2 个答案:

答案 0 :(得分:0)

将上下文传递给适配器,然后更改此行代码

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

 View v = LayoutInflater.from(context_passed_to_adapter)
.inflate(R.layout.card_heatmap, parent, false);

希望有所帮助。

答案 1 :(得分:0)

这里也有同样的问题。

        <ImageButton
...
            android:background="?selectableItemBackgroundBorderless"
...

必须从getApplicationContext更改为parent.getContext()。

@Override
public ListNameRVAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    //View v = LayoutInflater.from(mContext).inflate(R.layout.recycler_cardview_item_list_names,parent,false);
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_cardview_item_list_names,parent,false);

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