title: Unable to resolve java.lang.ClassCastException
body: <p>I am trying to develop my first android app but am stuck at the following error - 'java.lang.ClassCastException. Here is my java code:</p>
  
  <pre><code>package com.example.fresh24;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.List;
  
  public class CartAdapter extends RecyclerView.Adapter {
  
      private List&lt;CartItemModel&gt; cartItemModelList;
  
      public CartAdapter(List&lt;CartItemModel&gt; cartItemModelList) {
          this.cartItemModelList = cartItemModelList;
      }
  
      @Override
      public int getItemViewType(int position) {
          switch (cartItemModelList.get(position).getType()) {
              case 0:
                  return CartItemModel.CART_ITEM;
              case 1:
                  return CartItemModel.TOTAL_AMOUNT;
              default:
                  return -1;
          }
      }
  
      @NonNull
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
          switch (viewType) {
              case CartItemModel.CART_ITEM:
                  View cartItemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_item_layout, viewGroup, false);
                  return new CartItemViewHolder(cartItemView);
              case CartItemModel.TOTAL_AMOUNT:
                  View cartTotalView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_total_amount_layout, viewGroup, false);
                  return new CartItemViewHolder(cartTotalView);
              default:
                  return null;
          }
      }
  
      @Override
      public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
          switch (cartItemModelList.get(position).getType()) {
              case CartItemModel.CART_ITEM:
                  int resource = cartItemModelList.get(position).getProductImage();
                  String title = cartItemModelList.get(position).getProducTitle();
                  int freeCoupons = cartItemModelList.get(position).getFreeCoupons();
                  String productPrice = cartItemModelList.get(position).getProductPrice();
                  String cutPrice = cartItemModelList.get(position).getCutPrice();
                  int offersApplied = cartItemModelList.get(position).getOffersApplied();
                  ((CartItemViewHolder)viewHolder).setItemDetails(resource,title,freeCoupons,productPrice,cutPrice,offersApplied);
                  break;
              case CartItemModel.TOTAL_AMOUNT:
                  String totalItems = cartItemModelList.get(position).getTotalItems();
                  String totalItemsPrice = cartItemModelList.get(position).getTotalItemPrice();
                  String conveniencePrice = cartItemModelList.get(position).getConveniencePrice();
                  String totalAmount = cartItemModelList.get(position).getTotalAmount();
                  String savedAmount = cartItemModelList.get(position).getSavedAmount();
                  ((CartTotalAmountViewHolder)viewHolder).setTotalAmount(totalItems,totalItemsPrice,conveniencePrice,totalAmount,savedAmount);
                  break;
              default:
                  return;
          }
      }
  
      @Override
      public int getItemCount() {
          return cartItemModelList.size();
      }
  
      class CartItemViewHolder extends RecyclerView.ViewHolder {
          private ImageView productImage;
          private ImageView freecouponIcon;
          private TextView productTitle;
          private TextView freeCoupons;
          private TextView productPrice;
          private TextView productCutPrice;
          private TextView offersApplied;
          /*private TextView couponsApplied;
          private TextView productQuantity;*/
  
          public CartItemViewHolder(@NonNull View itemView) {
              super(itemView);
              productImage = itemView.findViewById(R.id.product_image);
              productTitle = itemView.findViewById(R.id.product_title);
              freecouponIcon = itemView.findViewById(R.id.free_coupon_icon);
              freeCoupons = itemView.findViewById(R.id.tv_free_coupon);
              productPrice = itemView.findViewById(R.id.product_price);
              productCutPrice = itemView.findViewById(R.id.cut_price);
              offersApplied = itemView.findViewById(R.id.offers_applied);
  
          }
  
          private void setItemDetails(int resource, String title, int freeCouponsNo, String productPriceText, String productCutPriceText, int offersAppliedNo) {
              productImage.setImageResource(resource);
              productTitle.setText(title);
              if (freeCouponsNo &gt; 0) {
                  freecouponIcon.setVisibility(View.VISIBLE);
                  freeCoupons.setVisibility(View.VISIBLE);
                  if (freeCouponsNo == 1) {
                      freeCoupons.setText("free " + freeCouponsNo + " Coupon");
                  } else {
                      freeCoupons.setText("free " + freeCouponsNo + " Coupons");
                  }
              } else {
                  freecouponIcon.setVisibility(View.INVISIBLE);
                  freeCoupons.setVisibility(View.INVISIBLE);
              }
              productPrice.setText(productPriceText);
              productCutPrice.setText(productCutPriceText);
              if (offersAppliedNo &gt; 0) {
                  offersApplied.setVisibility(View.VISIBLE);
                  offersApplied.setText(offersAppliedNo + "Offers Applied");
              } else {
                  offersApplied.setVisibility(View.INVISIBLE);
              }
          }
      }
  
      class CartTotalAmountViewHolder extends RecyclerView.ViewHolder {
  
          private TextView totalItems;
          private TextView totalItemPrice;
          private TextView conveniencePrice;
          private TextView totalAmount;
          private TextView savedAmount;
  
          public CartTotalAmountViewHolder(@NonNull View itemView) {
              super(itemView);
              totalItems = itemView.findViewById(R.id.total_items);
              totalItemPrice = itemView.findViewById(R.id.total_items_price);
              conveniencePrice = itemView.findViewById(R.id.convenience_price);
              totalAmount = itemView.findViewById(R.id.total_price);
              savedAmount = itemView.findViewById(R.id.saved_amount);
          }
  
          private void setTotalAmount(String totalItemText, String totalItemPriceText, String conveniencePriceText, String totalAmountText, String savedAmountText) {
              totalItems.setText(totalItemText);
              totalItemPrice.setText(totalItemPriceText);
              conveniencePrice.setText(conveniencePriceText);
              totalAmount.setText(totalAmountText);
              savedAmount.setText(savedAmountText);
          }
      }
  }
  </code></pre>
  
  <p>and here is the complete stack trace:</p>
  
  <pre><code>2019-08-01 09:51:45.298 26950-26950/com.example.fresh24 E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.fresh24, PID: 26950
      java.lang.ClassCastException: com.example.fresh24.CartAdapter$CartItemViewHolder cannot be cast to com.example.fresh24.CartAdapter$CartTotalAmountViewHolder
          at com.example.fresh24.CartAdapter.onBindViewHolder(CartAdapter.java:67)
          at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
          at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
          at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
          at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
          at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
          at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
          at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
          at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
          at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
          at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
          at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
          at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
          at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
          at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
          at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
          at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
  2019-08-01 09:51:45.298 26950-26950/com.example.fresh24 E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
          at android.view.View.layout(View.java:17523)
          at android.view.ViewGroup.layout(ViewGroup.java:5612)
          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
          at android.view.Choreographer.doCallbacks(Choreographer.java:683)
          at android.view.Choreographer.doFrame(Choreographer.java:619)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
          at android.os.Handler.handleCallback(Handler.java:751)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:154)
          at android.app.ActivityThread.main(ActivityThread.java:6077)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
  </code></pre>
  
  <p>I have gone through documentation regarding the CastException but I am unable to figure out the source of the error. Please help. Thanks in advance.</p>