Error inflating class androidx.cardview.widget.CardView on CardStackView library
我正在尝试实现该库。 这里编写的示例代码在Kotlin中,但是我的项目在Java中,因此我尝试将代码从kotlin转换为Java。 但是我收到以下错误:
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class androidx.cardview.widget.CardView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
在夸大cardview
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | class CardStackAdapter extends RecyclerView.Adapter<CardStackAdapter.ViewHolder> { private Context context; private ArrayList<Spot> listItem; public CardStackAdapter(Context applicationContext, ArrayList<Spot> listItem) { this.context = applicationContext; this.listItem = listItem; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false); // ----> I am getting error here.. return new ViewHolder(context, view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { final String name = listItem.get(position).name; String url = listItem.get(position).URL; String city = listItem.get(position).city; holder.name.setText(name); holder.city.setText(city); Picasso.get().load(url).into(holder.image); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, name, Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return listItem != null ? listItem.size() : 0; } public void setItem(ArrayList<Spot> listItem){ this.listItem = listItem; } public ArrayList<Spot> getItem(){ return listItem; } class ViewHolder extends RecyclerView.ViewHolder { TextView name; TextView city; ImageView image; ViewHolder(Context context, View view) { super(view); this.name = view.findViewById(R.id.item_name); this.city = view.findViewById(R.id.item_city); this.image = view.findViewById(R.id.item_image); } } } |
我对此cardview的XML代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | <?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground" app:cardUseCompatPadding="true" app:cardPreventCornerOverlap="false" app:cardCornerRadius="8dp" app:cardBackgroundColor="@android:color/white"> <com.makeramen.roundedimageview.RoundedImageView android:id="@+id/item_image" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" app:riv_corner_radius="8dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:padding="16dp" android:background="@drawable/gradation_black"> <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textColor="@android:color/white" android:textSize="26sp"/> <TextView android:id="@+id/item_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:textColor="@android:color/white" android:textSize="20sp"/> </LinearLayout> <FrameLayout android:id="@+id/left_overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/overlay_black"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/skip_white_120dp" android:layout_gravity="center"/> </FrameLayout> <FrameLayout android:id="@+id/right_overlay" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/overlay_black"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/like_white_120dp" android:layout_gravity="center"/> </FrameLayout> <FrameLayout android:id="@+id/top_overlay" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <FrameLayout android:id="@+id/bottom_overlay" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </androidx.cardview.widget.CardView> |
我已经在Github仓库上上传了这个项目。 任何建议都是最欢迎的。 谢谢!
删除所附图像中在此处标记的两行**,因为旧的appcompact支持库中包含的这两行不包含在androidx.appcompact中,并且视图持有人无法膨胀,因为这两行我遇到了相同的问题,通过几种方法解决它,将cardview更改为线性,并使之工作,然后尝试从card view中删除一些行,现在可以完美地工作了
用这个:
1 2 3 4 5 6 7 8 9 | <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" app:cardUseCompatPadding="true" app:cardPreventCornerOverlap="false" app:cardCornerRadius="8dp" app:cardBackgroundColor="@android:color/white"> |
代替
1 2 3 4 5 6 7 8 9 10 11 | <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="?attr/selectableItemBackground" android:foreground="?attr/selectableItemBackground" app:cardUseCompatPadding="true" app:cardPreventCornerOverlap="false" app:cardCornerRadius="8dp" app:cardBackgroundColor="@android:color/white"> |
为了保持对AndroidX使用的困惑
1 2 | android:background="?android:attr/selectableItemBackground" android:foreground="?android:attr/selectableItemBackground" |
使用android:attr应用为应用程序的当前主题定义的属性
我也遇到了同样的问题,并找到了解决方案。检查CardView库是否可用,如果没有,则添加它。
如何添加
以我为例,我试图在低端设备上设置图像以使用
1 | android:background="drawable/temp_image" |
如果尝试使用背景或前景设置图像,则将其删除。