首页 > 解决方案 > 我收到此错误:java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView 无法转换为 android.widget.TextView

问题描述

我收到此错误

java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView 不能在 com.prerak.agriapp.PopularAdapter$ImageViewHolder.(PopularAdapter.java:69) 在 com.prerak.agriapp.PopularAdapter 的 android.widget.TextView .onCreateViewHolder(PopularAdapter.java:36) 在 com.prerak.agriapp.PopularAdapter.onCreateViewHolder(PopularAdapter.java:16)

流行适配器.java


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.util.List;

public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {

    private Context mContext;
    private List<Popular> mPopular;

    public PopularAdapter (Context context, List<Popular> populars)
    {

        this.mContext=context;
        this.mPopular=populars;

    }



    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

       View v= LayoutInflater.from(mContext).inflate(R.layout.popular_item, parent, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int i) {

        Popular popularCur = mPopular.get(i);
        holder.prod_name.setText(popularCur.getProduct_title());
        holder.prod_price.setText(popularCur.getProduct_price());
        Picasso.with(mContext)
                .load(popularCur.getProduct_image())
                .placeholder(R.drawable.ic_stub)
                .fit()
                .centerCrop()
                .into((Target) holder.prod_img);

    }

    @Override
    public int getItemCount() {
        return mPopular.size();
    }

    public class ImageViewHolder extends RecyclerView.ViewHolder{

        public TextView prod_name, prod_price, prod_img;


        public ImageViewHolder(View itemView) {
            super(itemView);

            prod_name = itemView.findViewById(R.id.prodName);
            prod_price = itemView.findViewById(R.id.prodPrice);
            prod_img = itemView.findViewById(R.id.prodImage);

        }
    }
}

流行项目.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp">


    <android.support.v7.widget.CardView
        android:layout_width="120dp"
        android:layout_height="160dp"
        android:layout_marginBottom="15dp"
        android:elevation="6dp"
        app:cardCornerRadius="6dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:scaleType="centerCrop"
                android:id="@+id/prodImage"
                android:src="@drawable/ic_stub"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:id="@+id/prodName"
                android:textColor="@color/primary_dark"
                android:text="Product"
                />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="10dp"
                android:background="#ffffff"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal">


                <ImageView
                    android:layout_width="0dp"
                    android:layout_height="18dp"
                    android:layout_weight="1"
                    android:src="@drawable/iconfarm"
                    android:tint="@color/primary_dark"/>

                <TextView
                    android:layout_width="1dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="3"
                    android:gravity="right"
                    android:paddingRight="10dp"
                    android:text="$200"
                    android:id="@+id/prodPrice"
                    android:textColor="@color/primary_dark"
                    />

            </LinearLayout>


        </LinearLayout>





    </android.support.v7.widget.CardView>

</LinearLayout>

activity_post.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PostActivity">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:text="Popular Products"
                    android:textColor="@color/primary_dark"
                    android:textSize="24sp"
                    />

            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="10dp">

                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/recycler_view"/>


            </LinearLayout>


        </LinearLayout>

    </ScrollView>







</LinearLayout>

标签: android

解决方案


您正在尝试将 prod_image 转换为 TextView,它原本是 XML 布局中的 ImageView。

public class ImageViewHolder extends RecyclerView.ViewHolder{

        public TextView prod_name, prod_price;
        public ImageView prod_img;


        public ImageViewHolder(View itemView) {
            super(itemView);

            prod_name = itemView.findViewById(R.id.prodName);
            prod_price = itemView.findViewById(R.id.prodPrice);
            prod_img = itemView.findViewById(R.id.prodImage);

        }
    }

推荐阅读