简体   繁体   English

我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView

[英]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这是我的java代码

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)这是我得到的错误:java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 无法转换为 android.widget.TextView at com.example.movi​​ebookingapp2.MovieAdapter$ViewHolder.(MovieAdapter.java:27) at com .example.movi​​ebookingapp2.MovieAdapter.onCreateViewHolder(MovieAdapter.java:46) 在 com.example.movi​​ebookingapp2.MovieAdapter.onCreateViewHolder(MovieAdapter.java:11)

Change the reference for rowImage from将 rowImage 的引用更改为

 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我认为您可能在 UI 中使用了imageView ,但在适配器中,您将其定义为textView

please first see the XML file and check TextView rowImage请先查看XML文件并检查TextView rowImage

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.ScrollView无法强制转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton 不能转换为 android.widget.Button - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法转换为android.widget.TextView - java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView 错误 - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout 无法转换为 androidx.appcompat.widget.Toolbar - Error - java.lang.ClassCastException:com.google.android.material.appbar.AppBarLayout cannot be cast to androidx.appcompat.widget.Toolbar java.lang.ClassCastException:android.widget.TextView - java.lang.ClassCastException: android.widget.TextView 如何修复使用:java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView 不能转换为 android.widget.Button - how to fix aused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to android.widget.Button java.lang.RuntimeException:在类 androidx.appcompat.widget.AppCompatImageView 上发现名称 isImportantForAccessibility 的 getter 冲突 - java.lang.RuntimeException: Found conflicting getters for name isImportantForAccessibility on class androidx.appcompat.widget.AppCompatImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM