【问题标题】:Recycler view adapter InflateExceptionRecycler 视图适配器 InflateException
【发布时间】:2017-09-09 21:24:19
【问题描述】:

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

这是导致崩溃的方法:

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

提前致谢。

【问题讨论】:

  • 分享HomeAdapter类的代码
  • 哦,是的,对不起,我想包括那个。我把它添加到帖子中

标签: android xml android-recyclerview adapter


【解决方案1】:

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

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

希望对您有所帮助。

【讨论】:

  • 如果有帮助请告诉我。
  • RecyclerView.Adapter 没有“getContext()”方法,这就是我必须使用 ViewGroup 父级的原因。我将尝试在构造函数中将上下文传递给适配器并使用它来扩展视图。我会看看这是否会改变什么
  • 是的,你确实把答案改成了我刚才所说的。
  • 是的。希望有帮助。您的例外不是您正在使用的 xml 或前景,而是上下文:)
  • @swerly 运气好吗?
【解决方案2】:

同样的问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多