在 RecyclerView 上膨胀类 ImageView 时出错
Posted
技术标签:
【中文标题】在 RecyclerView 上膨胀类 ImageView 时出错【英文标题】:Error inflating Class ImageView on RecyclerView 【发布时间】:2017-03-06 02:57:48 【问题描述】:我构建了一个最低 SDK 15 和目标 23 的应用程序,我在我的 RecyclerView
中有不同的看法。
应用程序在 android 版本 => 5.0 上运行良好,但在 android 版本
二进制 XML 文件第 159 行:膨胀类 ImageView 时出错 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:749) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:489) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 在 com.shixxels.thankgodrichard.spotpopfinal.adapter.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:64) 在 com.shixxels.thankgodrichard.spotpopfinal.adapter.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:27) 在 android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5833) 在 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5057) 在 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4967) 在 android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029) 在 android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414) 在 android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377) 在 android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578) 在 android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315) 在 android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124) 在 android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3568) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 在 android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1652) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1436) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131) 在 android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 在 android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364) 在 android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:852) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1197) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:448) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGroup.java:4373) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 在 android.view.View.layout(View.java:14008) 在 android.view.ViewGroup.layout(ViewGr
下面是我的RecyclerAdapter
的代码:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> // line 27
private static final int Image_digit = 1;
private static final int Map_digit = 2;
private static final int Text_digit = 3;
private Context context;
private List<FeedAdapter> mDataList;
private LayoutInflater inflater;
public RecyclerAdapter(Context context, List<FeedAdapter> data)
inflater = LayoutInflater.from(context);
this.mDataList = data;
this.context = context;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
switch (viewType) // Create the Prime and Non-Prime row Layouts
case Image_digit:
ViewGroup imageView = (ViewGroup) inflater.inflate(R.layout.list_item, parent, false);
MyViewHolder_Images holderPrime = new MyViewHolder_Images(imageView);
return holderPrime;
case Map_digit:
ViewGroup mapView = (ViewGroup) inflater.inflate(R.layout.list_item_map, parent, false);
MyViewHolder_Map holderNonPrime = new MyViewHolder_Map(mapView);
return holderNonPrime;
default:
ViewGroup defaultRow = (ViewGroup) inflater.inflate(R.layout.list_item_text, parent, false); // line 64
MyViewHolder_Text holderDefault = new MyViewHolder_Text(defaultRow);
return holderDefault;
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
FeedAdapter current = mDataList.get(position);
switch (holder.getItemViewType())
case Image_digit:
MyViewHolder_Images holder_prime = (MyViewHolder_Images) holder;
holder_prime.setData(current);
break;
case Map_digit:
MyViewHolder_Map holder_not_prime = (MyViewHolder_Map) holder;
holder_not_prime.setData(current);
break;
default:
MyViewHolder_Text holder_not_text = (MyViewHolder_Text) holder;
holder_not_text.setData(current);
break;
@Override
public int getItemCount()
return mDataList.size();
@Override // This will help us to determine ROW TYPE : i.e. the PRIME or NON-PRIME row.
public int getItemViewType(int position)
FeedAdapter feedAdapter = mDataList.get(position);
if (feedAdapter.getViewInt() == 1)
return Image_digit;
else if (feedAdapter.getViewInt() == 2)
return Map_digit;
else if (feedAdapter.getViewInt() == 3)
return Text_digit;
else
return 0;
class MyViewHolder extends RecyclerView.ViewHolder
public MyViewHolder(View itemView)
super(itemView);
// Holder class for Images rows
public class MyViewHolder_Images extends MyViewHolder implements View.OnClickListener
TextView title;
ImageView imgThumb, imgRowType, menu;
public MyViewHolder_Images(View itemView)
super(itemView);
title = (TextView) itemView.findViewById(R.id.tvTitle);
imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
imgRowType = (ImageView) itemView.findViewById(R.id.img_row2);
menu = (ImageView) itemView.findViewById(R.id.img_row_delete);
public void setData(final FeedAdapter current)
this.title.setText(current.getTitle());
this.imgThumb.setImageResource(current.getImageID());
this.imgRowType.setImageResource(R.drawable.thumb_1_4);
this.menu.setOnClickListener(MyViewHolder_Images.this);
this.imgThumb.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showImage(current.getImageID());
);
@Override
public void onClick(View v)
Log.i("onClick"," i was clicked");
openDialog();
// Holder class for maps rows
public class MyViewHolder_Map extends MyViewHolder implements View.OnClickListener
TextView title;
ImageView imgThumb, imgRowType, menu;
public MyViewHolder_Map(View itemView)
super(itemView);
title = (TextView) itemView.findViewById(R.id.tvTitle);
imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
imgRowType = (ImageView) itemView.findViewById(R.id.img_row2);
menu = (ImageView) itemView.findViewById(R.id.img_row_delete);
public void setData(final FeedAdapter current)
this.title.setText(current.getTitle());
this.imgThumb.setImageResource(current.getImageID());
this.imgRowType.setImageResource(R.drawable.thumb_1_4);
this.menu.setOnClickListener(MyViewHolder_Map.this);
this.imgThumb.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showImage(current.getImageID());
);
@Override
public void onClick(View v)
Log.i("onClick","i was clicked");
openDialog();
// Holder class for NON-Text rows
public class MyViewHolder_Text extends MyViewHolder implements View.OnClickListener
TextView title;
ImageView imgThumb, menu;
public MyViewHolder_Text(View itemView)
super(itemView);
title = (TextView) itemView.findViewById(R.id.tvTitle);
imgThumb = (ImageView) itemView.findViewById(R.id.img_row);
menu = (ImageView) itemView.findViewById(R.id.img_row_delete);
public void setData(final FeedAdapter current)
this.title.setText(current.getTitle());
this.imgThumb.setImageResource(current.getImageID());
this.menu.setOnClickListener(MyViewHolder_Text.this);
this.imgThumb.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showImage(current.getImageID());
);
@Override
public void onClick(View v)
Log.i("onClick","i was clicked");
openDialog();
public void openDialog()
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout.LayoutParams dialogParams = new LinearLayout.LayoutParams(
400, 256);//set height and width here, ie (width,height)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dislogView = inflater
.inflate(R.layout.feed_menu, null);
dialog.setContentView(dislogView, dialogParams);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
TextView dialogButton = (TextView) dialog.findViewById(R.id.report);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
dialog.dismiss();
);
dialog.show();
public void showImage(int imageUri)
Dialog builder = new Dialog(context);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
builder.setOnDismissListener(new DialogInterface.OnDismissListener()
@Override
public void onDismiss(DialogInterface dialogInterface)
//nothing;
);
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageUri);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
list_item_text.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"
android:layout_
android:layout_
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:background="@android:color/white"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_row"
android:layout_
android:layout_
android:scaleType="fitXY"
android:layout_margin="7dp"
android:src="@drawable/thumb_1_0"/>
<LinearLayout
android:layout_
android:layout_
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal"
android:layout_marginTop="4dp">
<TextView
android:id="@+id/tvTitle"
android:layout_
android:layout_
android:text="@string/title"
android:textColor="#000000"
android:textSize="12sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_user_tag"
android:layout_
android:layout_
android:text="@string/tag"
android:textColor="#616161"
android:textSize="9sp"
android:layout_marginLeft="5dp" />
</LinearLayout>
<TextView
android:id="@+id/tvDescription"
android:layout_
android:layout_
android:text="@string/description"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textSize="9sp"
android:textStyle="italic"/>
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<View
android:layout_
android:layout_
android:background="#FF0000FF" />
<TextView
android:layout_
android:layout_
android:layout_marginLeft="5dp"
android:text="@string/lorem"
android:maxLines="2"
android:ellipsize="end"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<ImageView
android:id="@+id/img_row_delete"
android:layout_
android:layout_
android:layout_margin="7dp"
android:contentDescription="@string/delete_image"
android:src="@drawable/more"
android:tint="@color/colorPrimary"
android:clickable="true" />
</LinearLayout>
</LinearLayout>
<View
android:layout_
android:layout_marginTop="5dp"
android:layout_marginLeft="6.3dp"
android:layout_marginRight="6.3dp"
android:layout_
android:background="@color/colorPrimary" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:id="@+id/time_of_post"
android:text="about 5 min. ago"
android:layout_
android:layout_
android:layout_margin="7dp"/>
<RelativeLayout
android:layout_
android:layout_
android:layout_marginLeft="45dp"
android:layout_marginRight="10dp"
android:layout_marginTop="3dp">
<ImageView
android:layout_
android:id="@+id/comment_count_img"
android:src="@drawable/comment_count"
android:layout_
android:layout_marginRight="3dp"
android:layout_toLeftOf="@+id/comment_count_no"
android:clickable="true" />
<TextView
android:layout_
android:layout_
android:id="@+id/comment_count_no"
android:text="10"
android:layout_toLeftOf="@+id/comment_like_img"
android:layout_marginRight="5dp"/>
<ImageView
android:layout_
android:id="@+id/comment_like_img"
android:src="@drawable/like_count"
android:layout_marginLeft="6.7dp"
android:layout_
android:layout_toLeftOf="@+id/comment_like_no"
android:layout_marginRight="3dp"
android:clickable="true" />
<TextView
android:layout_
android:layout_
android:id="@+id/comment_like_no"
android:text="10"
android:layout_toLeftOf="@+id/comment_dislike_img"
android:layout_marginRight="5dp"/>
<ImageView
android:layout_
android:id="@+id/comment_dislike_img"
android:src="@drawable/thumbs_down"
android:layout_
android:layout_marginLeft="6.7dp"
android:layout_toLeftOf="@+id/comment_dislike_no"
android:layout_marginRight="3dp"
android:clickable="true" />
<TextView
android:layout_
android:layout_
android:id="@+id/comment_dislike_no"
android:text="10"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
build.gradle
apply plugin: 'com.android.application'
android
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig
applicationId "com.shixxels.thankgodrichard.spotpopfinal"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
multiDexEnabled true
buildTypes
release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
dexOptions
javaMaxHeapSize "4g"
【问题讨论】:
最好也向我们展示 XML。 显示您的 recyclerview 项目的 XML 文件 发布您的应用级别build.gradle
文件。
指出你的com.shixxels.thankgodrichard.spotpopfinal.adapter.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:64)
和com.shixxels.thankgodrichard.spotpopfinal.adapter.RecyclerAdapter.onCreateViewHolder(RecyclerAdapter.java:27)
。还显示围绕这些行使用的 XML。
请在上面的代码中指出你的第 64 行和第 27 行。
【参考方案1】:
如果您在 ImageView 中使用矢量图标作为背景,并且 android 版本低于 5.0,则 imageview 会导致此崩溃。尝试使用<android.support.v7.widget.AppCompatImageView
而不是ImageView
,并且不要忘记在您的Java 代码中声明并使用appCompatImageView 投射您的imageview。
要将矢量设置为背景,请使用此 xml 属性:app:srcCompat
选择颜色使用 android:tint
,最后在您的 gradle 文件中,您必须添加对矢量的支持:
android
//...
defaultConfig
//...
vectorDrawables
useSupportLibrary = true
【讨论】:
【参考方案2】:看起来你实际上并没有夸大ImageView
。
private List<FeedAdapter> mDataList;
private LayoutInflater inflater;
public RecyclerAdapter(Context context, List<FeedAdapter> data)
inflater = LayoutInflater.from(context);
this.mDataList = data;
this.context = context;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
switch (viewType) // Create the Prime and Non-Prime row Layouts
case Image_digit:
ViewGroup myLayout = (ViewGroup) inflater.inflate(R.layout.list_item, parent, false);
ImageView myImage = (ImageView) myLayout.findViewById(R.id.my_image_id);
MyViewHolder_Images holderPrime = new MyViewHolder_Images(myImage);
return holderPrime;
【讨论】:
我不仅仅是在膨胀 ImageView。还有其他观点。这就是我使用 ViewGroup 的原因以上是关于在 RecyclerView 上膨胀类 ImageView 时出错的主要内容,如果未能解决你的问题,请参考以下文章
膨胀类 android.support.v7.widget.RecyclerView 时出错
膨胀类 android.support.v7.widget.RecyclerView(Eclipse) 时出错