简体   繁体   中英

androidx.appcompat.widget.AppCompatImageButton cannot be cast to androidx.cardview.widget.CardView

I have a problem with an adapter on Android, I get the following error:

 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to androidx.cardview.widget.CardView

My java file is:

package com.mastersuaw.cuevana3premium.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mastersuaw.cuevana3premium.R;
import com.mastersuaw.cuevana3premium.models.NavigationModel;
import java.util.List;

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.OriginalViewHolder> {
  final private Context ctx;
  
  final private List<NavigationModel> items;
  
  private OnItemClickListener mOnItemClickListener;
  
  final private String menuStyle;
  
  OriginalViewHolder viewHolder;
  
  public NavigationAdapter(Context paramContext, List<NavigationModel> paramList, String paramString) {
    this.items = paramList;
    this.ctx = paramContext;
    this.menuStyle = paramString;
  }
  
  public static int getImageId(Context paramContext, String paramString) {
    Resources resources = paramContext.getResources();
    String stringBuilder = "drawable/" +
            paramString;
    return resources.getIdentifier(stringBuilder, null, paramContext.getPackageName());
  }

  public void chanColor(OriginalViewHolder paramOriginalViewHolder, int paramInt1, int paramInt2) {
    if (paramInt1 != 0)
      if (this.menuStyle.equals("grid")) {
        this.viewHolder.cardView.setCardBackgroundColor(this.ctx.getResources().getColor(paramInt2));
        this.viewHolder.name.setTextColor(this.ctx.getResources().getColor(R.color.grey_60));
      } else {
        this.viewHolder.name.setTextColor(this.ctx.getResources().getColor(R.color.default_text));
        this.viewHolder.selectedLayout.setBackgroundColor(this.ctx.getResources().getColor(R.color.white));
      }  
    if (paramOriginalViewHolder != null)
      if (this.menuStyle.equals("grid")) {
        paramOriginalViewHolder.cardView.setCardBackgroundColor(this.ctx.getResources().getColor(paramInt2));
        paramOriginalViewHolder.name.setTextColor(this.ctx.getResources().getColor(R.color.grey_60));
      } else {
        paramOriginalViewHolder.name.setTextColor(this.ctx.getResources().getColor(R.color.default_text));
        paramOriginalViewHolder.selectedLayout.setBackgroundColor(this.ctx.getResources().getColor(R.color.white));
      }  
  }
  
  public int getItemCount() {
    return this.items.size();
  }

  public void onBindViewHolder(final OriginalViewHolder holder, final int position) {
    NavigationModel navigationModel = this.items.get(position);
    if (position == 0) {

      this.viewHolder = holder;

      if (this.menuStyle.equals("grid")) {
        holder.cardView.setCardBackgroundColor(this.ctx.getResources().getColor(R.color.colorPrimary));
        holder.name.setTextColor(this.ctx.getResources().getColor(R.color.white));
      } else {
        holder.selectedLayout.setBackground(this.ctx.getResources().getDrawable(R.drawable.round_grey_transparent));holder.name.setTextColor(this.ctx.getResources().getColor(R.color.colorPrimary));
      }
    } 
    holder.name.setText(navigationModel.getTitle());
    holder.image.setImageResource(getImageId(this.ctx, navigationModel.getImg()));
    if (this.menuStyle.equals("grid")) {
      holder.cardView.setOnClickListener(param1View -> {
        if (NavigationAdapter.this.mOnItemClickListener != null)
          NavigationAdapter.this.mOnItemClickListener.onItemClick(param1View, NavigationAdapter.this.items.get(position), position, holder);
      });
    } else {
      holder.itemLayout.setOnClickListener(param1View -> {
        if (NavigationAdapter.this.mOnItemClickListener != null)
          NavigationAdapter.this.mOnItemClickListener.onItemClick(param1View, NavigationAdapter.this.items.get(position), position, holder);
      });
    } 
  }


  @Override
  public OriginalViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
    View view = null;
    if (this.menuStyle.equals("grid")) {
      view = LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.card_nav_view, paramViewGroup, false);
    } else {
      view = LayoutInflater.from(view.getContext()).inflate(R.layout.card_nav_view, (ViewGroup)view, false);
    } 
    return new OriginalViewHolder(view);
  }
  
  public void setOnItemClickListener(OnItemClickListener paramOnItemClickListener) {
    this.mOnItemClickListener = paramOnItemClickListener;
  }
  
  public interface OnItemClickListener {
    void onItemClick(View param1View, NavigationModel param1NavigationModel, int param1Int, OriginalViewHolder param1OriginalViewHolder);
  }
  public class OriginalViewHolder extends RecyclerView.ViewHolder {
    public CardView cardView;
    
    public ImageView image;
    
    public LinearLayout itemLayout;
    
    public TextView name;
    
    public LinearLayout selectedLayout;

    public OriginalViewHolder(View param1View) {
      super(param1View);
      this.image = param1View.findViewById(R.id.image);
      this.name = param1View.findViewById(R.id.name);

      if (NavigationAdapter.this.menuStyle.equals("grid")) {

        cardView = param1View.findViewById(R.id.card_view_home);
      } else {
        this.itemLayout = param1View.findViewById(R.id.item_layout);
        this.selectedLayout = param1View.findViewById(R.id.selected_layout);
      }

    }
  }
}

And my res is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:id="@id/card_view_home" android:padding="10.0dip" android:layout_width="90.0dip" android:layout_height="70.0dip" android:layout_margin="10.0dip" app:cardElevation="4.0dip"
  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout android:gravity="center" android:layout_gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <TextView android:textAppearance="@style/TextAppearance.Medium.Bold" android:textColor="@color/grey_60" android:gravity="center" android:id="@id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </LinearLayout>
</androidx.cardview.widget.CardView>

I don't know why this is happening to me, let's see if someone can help me out, thank you!

PD:

I am trying to make a list of cards with images and text and it is rare that I get this error, I do not know how to correct it, thank you very much.

As the error says ImageButton can't be cast to CardView. You are trying to get the image view button as cardview in your OriginalViewHolder class where you initialising the views using findViewById please check you xml id's accordingly.

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