6

I am trying to implement this library. The sample code which is written here is in Kotlin but my project was in Java so I tried converting the code from kotlin to java. But I am getting the following error :

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)

in inflating the cardview

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

}

My XML code for this cardview is :

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

I have uploaded this project on Github Repository. Any suggestion is most welcome. Thanks!

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
Piyush
  • 492
  • 5
  • 22

4 Answers4

9

delete the two lines marked here in the attached image** because these 2 lines included to the old appcompact support lib not included in androidx.appcompact and the view holder can't inflate because these 2 lines, I had the same problem and I tried to solve it by several ways change cardview to linear and makes it worked and after that tried to delete some lines from the card view and it works now perfectly

Use this:

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

instead of

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

see the image i marked lines here

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
mahmoud Zahran
  • 354
  • 2
  • 15
  • you're welcome.;) just you have to think out of box to solve the problem like changing to another view "linear layout" in this case trying more ways to catch the problem @Piyush then you can solve it so first catch second solve. Thanks! – mahmoud Zahran Oct 20 '19 at 10:28
1

To keep the riddle effect on AndroidX use

android:background="?android:attr/selectableItemBackground"
android:foreground="?android:attr/selectableItemBackground"

use of android:attr applies the attribute defined for the current theme of the app

dianakarenms
  • 2,609
  • 1
  • 22
  • 22
0

In my case i am trying to set an image on low end devices for card view using

android:background="drawable/temp_image"

If you are trying to set an image using background or foreground then remove it.

Aditya Patil
  • 1,287
  • 12
  • 19
  • Well I have marked the right answer!! Which solved my issue. I have posted the github repo for that project in java – Piyush Feb 13 '20 at 05:42
0

I've also had the same problem and have found a solution. Check if CardView library is available, if not then add it.

How to add it