简体   繁体   中英

i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView

 <?xml version="1.0" encoding="utf-8"?>
    <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="wrap_content">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:srcCompat="@tools:sample/avatars" />

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textSize="20sp"
            android:id="@+id/text1"
            android:layout_margin="10dp">

        </TextView>

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Description"
            android:textSize="20sp">

        </TextView>

    </LinearLayout>
  </LinearLayout>

this is my java code

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
    Context context;
    String[] movieNameList;
    String[] movieDescriptionList;
    int[] movieImages;


    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView rowName;
        TextView rowDescription;
        TextView rowImage;

        public ViewHolder(View itemView){
            super(itemView);
            rowName = itemView.findViewById(R.id.text1);
            rowDescription = itemView.findViewById(R.id.text2);
            rowImage = itemView.findViewById(R.id.imageView3);
        }

    }

    public MovieAdapter(Context context,String[] movieNameList,String[] movieDescriptionList,int[] 
     movieImages){
        this.context = context;
        this.movieNameList = movieNameList;
        this.movieDescriptionList = movieDescriptionList;
        this.movieImages = movieImages;
    }



@NonNull
@Override
    public MovieAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.firstpage,parent,false);
        return new ViewHolder(view);

    }

@Override
    public void onBindViewHolder(@NonNull MovieAdapter.ViewHolder holder, int position) {
        holder.rowName.setText(movieNameList[position]);
     }

@Override
    public int getItemCount() {
        return movieNameList.length ;
    }
}

this is the error i am getting: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView at com.example.moviebookingapp2.MovieAdapter$ViewHolder.(MovieAdapter.java:27) at com.example.moviebookingapp2.MovieAdapter.onCreateViewHolder(MovieAdapter.java:46) at com.example.moviebookingapp2.MovieAdapter.onCreateViewHolder(MovieAdapter.java:11)

Change the reference for rowImage from

 TextView rowImage;

To

 ImageView rowImage;  



 

I think you are probably using an imageView in the UI, but in the adapter, you defined it as textView

please first see the XML file and check TextView rowImage

   public static class ViewHolder extends RecyclerView.ViewHolder{
        ...
        TextView rowImage;
    
        public ViewHolder(View itemView){
            super(itemView);
            ...
            rowImage = itemView.findViewById(R.id.imageView3);
        }
    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM