java.lang.ClassCastException 在 RecyclerView 中,ViewHolder

java.lang.ClassCastException In RecycleView, ViewHolder

我在我的 RecyclerView class 中创建了两个 viewHolder,一个用于内容,另一个用于 facebook 原生广告,我的应用程序在 运行 之后立即发生冲突。我该如何克服这个问题?

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0:
                return new ViewHolderBoxOffice(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_movie_box_office, parent, false));
            case 2:

                return new NativeAdViewHolder(layoutInflater.from(parent.getContext()).inflate(R.layout.custom_ads_facebook, parent, false));

        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case 0:
                ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
                Movie currentMovie = mlistMovies.get(position);
                v_holder.movieTitle.setText(currentMovie.getTitle());
                Date movieReleaseDate = currentMovie.getReleaseDateTheater();
                if (movieReleaseDate != null) {
                    String formmattedDate = dateFormat.format(movieReleaseDate);
                    v_holder.movieReleaseDate.setText(formmattedDate);
                } else {
                    v_holder.movieReleaseDate.setText(Constants.NA);

                }
                int audienceScore = currentMovie.getAudienceScore();
                if (audienceScore == -1) {
                    v_holder.movieAudienceScore.setRating(0.0F);
                    v_holder.movieAudienceScore.setAlpha(0.5F);
                } else {
                    v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
                    v_holder.movieAudienceScore.setAlpha(1.0F);
                }
                String urlThumbnail = currentMovie.getUrlThumbnail();
                loadImages(urlThumbnail, v_holder);
            case 2:
                NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
                // Setting the Text.

                nativeAd.unregisterView();
                mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
                mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
                mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
                mHolder.nativeAdBody.setText(nativeAd.getAdBody());

                // Downloading and setting the ad icon.
                NativeAd.Image adIcon = nativeAd.getAdIcon();
                NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);

                // Download and setting the cover image.
                NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
                mHolder.nativeAdMedia.setNativeAd(nativeAd);


                // Add adChoices icon

               // if (adChoicesView == null)

                if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
                   AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);

                    mHolder.adview.addView(adChoicesView, 0);
                }

                nativeAd.registerViewForInteraction( mHolder.adview);

                default:
                    break;
        }
        if (position > previousPosition) {
            comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);

        } else {
            comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);

        }
        previousPosition = position;

    }



    private void loadImages(String urlThumbnail, final ViewHolderBoxOffice holder) {
        if (urlThumbnail != null) {
            imageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    holder.movieThumbnail.setImageBitmap(response.getBitmap());
                }

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
        }
    }


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

    @Override
    public void onError(Ad ad, AdError adError) {

    }

    @Override
    public void onAdLoaded(Ad ad) {


    }

    @Override
    public void onAdClicked(Ad ad) {
        L.m("Ads is loaded, Jeez man that is good");
        System.out.println("Loaded in fragment");
        nativeAd = manager.nextNativeAd();

        nativeAd.setAdListener(this);



    }
    @Override
    public int getItemViewType(int position) {
        int viewType = 0;
        if (position % 5 == 1)
            viewType = 2;
        return viewType;
    }

    @Override
    public void onAdsLoaded() {
        L.m("Ads is loaded, Jeez man that is good");
        System.out.println("Loaded in fragment");
        nativeAd = manager.nextNativeAd();

        nativeAd.setAdListener(this);


    }

    @Override
    public void onAdError(AdError adError) {

    }

    class NativeAdViewHolder extends RecyclerView.ViewHolder {
        private ImageView nativeAdIcon;
        private TextView nativeAdTitle;
        private TextView nativeAdBody;
        private MediaView nativeAdMedia;
        private TextView nativeAdSocialContext;
        private Button nativeAdCallToAction;
        private LinearLayout adview;


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

// Create native UI using the ad metadata.
            nativeAdIcon = (ImageView) itemView.findViewById(R.id.native_ad_icon);
            nativeAdTitle = (TextView) itemView.findViewById(R.id.native_ad_title);
            nativeAdBody = (TextView) itemView.findViewById(R.id.native_ad_body);
            nativeAdMedia = (MediaView) itemView.findViewById(R.id.native_ad_media);
            nativeAdSocialContext = (TextView) itemView.findViewById(R.id.native_ad_social_context);
            nativeAdCallToAction = (Button) itemView.findViewById(R.id.native_ad_call_to_action);
            adview=(LinearLayout) itemView.findViewById(R.id.ads_unit);


        }
    }


    class ViewHolderBoxOffice extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView movieThumbnail;
        private TextView movieTitle;
        private TextView movieReleaseDate;
        private RatingBar movieAudienceScore;

        public ViewHolderBoxOffice(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            movieThumbnail = (ImageView) itemView.findViewById(R.id.movieThumbnail);
            movieTitle = (TextView) itemView.findViewById(R.id.movieTitle);
            movieReleaseDate = (TextView) itemView.findViewById(R.id.movieReleaseDate);
            movieAudienceScore = (RatingBar) itemView.findViewById(R.id.movieAudienceScore);


        }

这是错误。

01-31 16:30:48.944 9429-9429/comeagain.materialdesign E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: comeagain.materialdesign, PID: 9429
                                                                        java.lang.ClassCastException: comeagain.materialdesign.adapters.AdapterBoxOffice$ViewHolderBoxOffice cannot be cast to comeagain.materialdesign.adapters.AdapterBoxOffice$NativeAdViewHolder
                                                                            at comeagain.materialdesign.adapters.AdapterBoxOffice.onBindViewHolder(AdapterBoxOffice.java:148)
                                                                            at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)

错误在行

 NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;

我无法确定问题是什么以及如何解决这个问题。请帮忙。

中断你的 Switch case。

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ViewHolderBoxOffice v_holder = (ViewHolderBoxOffice) holder;
            Movie currentMovie = mlistMovies.get(position);
            v_holder.movieTitle.setText(currentMovie.getTitle());
            Date movieReleaseDate = currentMovie.getReleaseDateTheater();
            if (movieReleaseDate != null) {
                String formmattedDate = dateFormat.format(movieReleaseDate);
                v_holder.movieReleaseDate.setText(formmattedDate);
            } else {
                v_holder.movieReleaseDate.setText(Constants.NA);

            }
            int audienceScore = currentMovie.getAudienceScore();
            if (audienceScore == -1) {
                v_holder.movieAudienceScore.setRating(0.0F);
                v_holder.movieAudienceScore.setAlpha(0.5F);
            } else {
                v_holder.movieAudienceScore.setRating(currentMovie.getAudienceScore() / 20.0F);
                v_holder.movieAudienceScore.setAlpha(1.0F);
            }
            String urlThumbnail = currentMovie.getUrlThumbnail();
            loadImages(urlThumbnail, v_holder);
            break;
        case 2:
            NativeAdViewHolder mHolder=(NativeAdViewHolder)holder;
            // Setting the Text.

            nativeAd.unregisterView();
            mHolder.nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
            mHolder.nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
            mHolder.nativeAdTitle.setText(nativeAd.getAdTitle());
            mHolder.nativeAdBody.setText(nativeAd.getAdBody());

            // Downloading and setting the ad icon.
            NativeAd.Image adIcon = nativeAd.getAdIcon();
            NativeAd.downloadAndDisplayImage(adIcon, mHolder.nativeAdIcon);

            // Download and setting the cover image.
            NativeAd.Image adCoverImage = nativeAd.getAdCoverImage();
            mHolder.nativeAdMedia.setNativeAd(nativeAd);


            // Add adChoices icon

           // if (adChoicesView == null)

            if(! (mHolder.adview.getChildAt(0) instanceof AdChoicesView)){
               AdChoicesView adChoicesView = new AdChoicesView(layoutInflater.getContext(), nativeAd, true);

                mHolder.adview.addView(adChoicesView, 0);
            }

            nativeAd.registerViewForInteraction( mHolder.adview);
            break;
          default:
                break;
    }
    if (position > previousPosition) {
        comeagain.materialdesign.anim.AnimationUtils.animate(holder, true);

    } else {
        comeagain.materialdesign.anim.AnimationUtils.animate(holder, false);

    }
    previousPosition = position;

}

下次使用调试器来查找类似的东西。