Home » Java » java – Inflating Images on ImageView on a RecylerView-Exceptionshub

java – Inflating Images on ImageView on a RecylerView-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have been having a problem inflating an images on an Image view in a recyclerview.The images fetched from firebase database into the recyclerview, Precisely i get the following error.

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cnatra_measure, PID: 21096
    android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class android.widget.ImageView
    Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class android.widget.ImageView
    Caused by: java.lang.reflect.InvocationTargetException

and a further in the error i get

at com.example.cnatra_measure.Adapters.Trads_StyleAdapter.onCreateViewHolder(Trads_StyleAdapter.java:54)
        at com.example.cnatra_measure.Adapters.Trads_StyleAdapter.onCreateViewHolder(Trads_StyleAdapter.java:21)

my Adapter code is given below

public class Trads_StyleAdapter extends RecyclerView.Adapter<Trads_StyleAdapter.Trads_ViewHolder> {

    private Context mCtx;
    private List<Styles> stylesList;
    public Trads_StyleAdapter(Context mCtx, List<Styles> stylesList) {
        this.mCtx = mCtx;
        this.stylesList = stylesList;
    }
    public Trads_StyleAdapter(ArrayList<Styles> stylesList) {
        this.stylesList = stylesList;
    }
    public static class Trads_ViewHolder extends RecyclerView.ViewHolder {
        ImageView styleImage;
        TextView style_name, style_price;
        //List<Food> foods;
        //Context ctx;
        public Trads_ViewHolder(View v) {
            super(v);
            styleImage = v.findViewById(R.id.styleIcon);
            style_name = v.findViewById(R.id.style_names);
           style_price = v.findViewById(R.id.style_price);
       }
}
    @NonNull
    @Override
    public Trads_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View v = inflater.inflate(R.layout.styles_items, parent, false);
        return new Trads_ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Trads_ViewHolder holder, int position) {
        Styles mStyles = stylesList.get(position);
        holder.style_name.setText(mStyles.getStyle_name());
        holder.style_price.setText(mStyles.getPrice());
        Picasso.get()
                .load(mStyles.getStyle_image())
                .placeholder(R.drawable.img_placeholder)
                .fit()
                .centerCrop()
                .into(holder.styleImage);
    }
    @Override
    public int getItemCount() {
        return stylesList.size();
    }
}

and the XML code for the list items is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:elevation="15dp"
        card_view:cardCornerRadius="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:id="@+id/iconWrapper"
            android:orientation="vertical">

            <ImageView
                android:layout_width="130dp"
                android:layout_height="130dp"
                android:id="@+id/styleIcon"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:layout_gravity="center_horizontal|center_vertical"
                android:scaleType="fitXY"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:focusable="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/style_names"
                android:fontFamily="@font/nunito_semibold"
                android:padding="5dp"
                android:textSize="20sp"
                android:textColor="@color/blue"
                android:textAlignment="center"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/naira"
                    android:fontFamily="@font/nunito_bold"
                    android:padding="5dp"
                    android:textSize="15sp"
                    android:textColor="@color/blue"
                    android:textAlignment="center"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/style_price"
                    android:padding="5dp"
                    android:textSize="15sp"
                    android:fontFamily="@font/nunito_bold"
                    android:textColor="@color/black"
                    android:textAlignment="center"/>
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</RelativeLayout>

Thanks in advance

How to&Answers:

Use this xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:elevation="15dp"
        card_view:cardCornerRadius="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:id="@+id/iconWrapper"
            android:orientation="vertical">

            <ImageView
                android:layout_width="130dp"
                android:layout_height="130dp"
                android:id="@+id/styleIcon"
                android:clickable="true"
                android:layout_gravity="center"
                android:scaleType="fitXY"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:focusable="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/style_names"
                android:fontFamily="@font/nunito_semibold"
                android:padding="5dp"
                android:textSize="20sp"
                android:textColor="@color/blue"
                android:textAlignment="center"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/naira"
                    android:fontFamily="@font/nunito_bold"
                    android:padding="5dp"
                    android:textSize="15sp"
                    android:textColor="@color/blue"
                    android:textAlignment="center"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/style_price"
                    android:padding="5dp"
                    android:textSize="15sp"
                    android:fontFamily="@font/nunito_bold"
                    android:textColor="@color/black"
                    android:textAlignment="center"/>
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
</RelativeLayout>