java.lang.ClassCastException:无法转换为RssLargeViewHolder

时间:2019-02-06 15:28:40

标签: java android

有人可以帮我吗?

当我尝试运行该应用程序时出现此错误(标题)。

这是完整的错误日志:

java.lang.ClassCastException: com.tinyapps.newsly.providers.rss.RssAdapter$HighlightViewHolder cannot be cast to com.tinyapps.newsly.providers.rss.RssAdapter$RssLargeViewHolder at com.tinyapps.newsly.providers.rss.RssAdapter.doBindViewHolder(RssAdapter.java:94)
at com.tinyapps.newsly.util.InfiniteRecyclerViewAdapter.onBindViewHolder(InfiniteRecyclerViewAdapter.java:102)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:606)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1769)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20831)
at android.view.ViewGroup.layout(ViewGroup.java:6203)

这是我的适配器代码:

public class RssAdapter extends InfiniteRecyclerViewAdapter {

    private List<RSSItem> objects;
    private Context context;

    private static int COMPACT = 0;
    private static int NORMAL = 1;
    private final static int HEADER_IMAGE= 2;

    private RSSItem item;

    private ViewModeUtils viewModeUtils;

    public RssAdapter(Context context,
                      List<RSSItem> list) {
        super(context, null);
        this.context = context;
        this.objects = list;

        this.viewModeUtils = new ViewModeUtils(context, RssFragment.class);
    }

    @Override
    public int getViewType(int position) {
        if (position == 0 || viewModeUtils.getViewMode() == ViewModeUtils.IMMERSIVE) {
            return HEADER_IMAGE;
        }
       else if (viewModeUtils.getViewMode() == ViewModeUtils.NORMAL) {
            return NORMAL;
        } else {
            return COMPACT;
        }
    }


    @Override
    protected RecyclerView.ViewHolder getViewHolder(ViewGroup parent, int viewType) {
        if (COMPACT == viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fragment_rss_row, parent, false);
            return new RssViewHolder((itemView));
        }  else if (viewType == HEADER_IMAGE) {
                View itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listview_highlight, parent, false);
                RecyclerView.ViewHolder holder = new HighlightViewHolder(itemView);
                requestFullSpan(holder);
                return holder;
        } else if (viewType == NORMAL) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.listview_row, parent, false);
            return new RssLargeViewHolder(itemView);
        }
        return null;
    }

    @Override
    protected void doBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof RssViewHolder){

            RssViewHolder holder = (RssViewHolder) viewHolder;

            holder.listTitle.setText(objects.get(position).getTitle());
            holder.listPubdate.setText(objects.get(position).getPubdate());
            holder.listThumb.setImageDrawable(null);
            String thumburl = objects.get(position).getThumburl();
            loadImageIntoView(thumburl, holder.listThumb);
            setOnClickListener(holder.itemView, position);

        } else {

            RssLargeViewHolder itemHolder = (RssLargeViewHolder) viewHolder;

            itemHolder.headlineView.setText(objects.get(position).getTitle());
            itemHolder.reportedDateView.setText(objects.get(position).getPubdate());
            itemHolder.imageView.setImageBitmap(null);
            String thumburl = objects.get(position).getThumburl();
            loadImageIntoView(thumburl, itemHolder.imageView);
            setOnClickListener(itemHolder.itemView, position);

        }
    }

    private void setOnClickListener(View view, final int position){
        view.setOnClickListener(view1 -> {
            HolderActivity.startWebViewActivity(context, objects.get(position).getLink(), Config.OPEN_INLINE_EXTERNAL, false, null);
        });
    }

    private void loadImageIntoView(String thumburl, final ImageView listThumb){
        if (thumburl != null && !thumburl.equals("")) {
            //setting the image
            final ImageView imageView = listThumb; // The view Picasso is loading an image into
            final Target target = new Target() {
                @Override
                public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                    /* Save the bitmap or do something with it here */

                    if (10 > bitmap.getWidth() || 10 > bitmap.getHeight()) {

                        Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.rounded_image_placeholder);

                        listThumb.setImageBitmap(bm);
                        listThumb.setImageResource(R.drawable.rounded_image_placeholder);
                    } else {
                        listThumb.setImageBitmap(bitmap);
                    }
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                }
            };

            imageView.setTag(target);
            Picasso.get()
                    .load(thumburl)
                    .into(target);
        } else {
            Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.rounded_image_placeholder);

            listThumb.setImageBitmap(bm);
            listThumb.setImageResource(R.drawable.rounded_image_placeholder);
        }
    }


    @Override
    protected int getCount() {
        return objects.size();
    }

    private class RssViewHolder extends RecyclerView.ViewHolder {
        TextView listTitle;
        TextView listPubdate;
        ImageView listThumb;

        RssViewHolder(View view){
            super(view);
            this.listTitle = view.findViewById(R.id.listtitle);
            this.listPubdate = view.findViewById(R.id.listpubdate);
            this.listThumb = view.findViewById(R.id.listthumb);
        }
    }

    private static class RssLargeViewHolder extends RecyclerView.ViewHolder {
        TextView headlineView;
        TextView reportedDateView;
        ImageView imageView;

        RssLargeViewHolder(View view){
            super(view);

            this.headlineView = view.findViewById(R.id.title);
            this.reportedDateView = view.findViewById(R.id.date);
            this.imageView = view.findViewById(R.id.thumbImage);

        }
    }

    private static class HighlightViewHolder extends RecyclerView.ViewHolder {
        TextView listTitle;
        TextView listPubdate;
        ImageView listThumb;
        TextView headlineView;
        TextView reportedDateView;
        ImageView imageView;

        HighlightViewHolder(View view){
            super(view);

            this.listTitle = view.findViewById(R.id.listtitle);
            this.listPubdate = view.findViewById(R.id.listpubdate);
            this.listThumb = view.findViewById(R.id.listthumb);
            this.headlineView = view.findViewById(R.id.title);
            this.reportedDateView = view.findViewById(R.id.date);
            this.imageView = view.findViewById(R.id.thumbImage);
        }
    }
}

我正在尝试做的事情: 因此,这是一个新闻应用程序,可在RecyclerView中显示提要,但我希望最新的帖子具有与其余帖子不同的静态UI,所以这是我想出的但出现此错误。

感谢您的帮助!谢谢。

2 个答案:

答案 0 :(得分:1)

您的树类型为RecyclerView.ViewHolder

  • RssViewHolder
  • RssLargeViewHolder
  • HighlightViewHolder

但是,在doBindViewHolder,您只是检查其中之一。

protected void doBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    if (viewHolder instanceof RssViewHolder){
        RssViewHolder itemHolder = (RssViewHolder) viewHolder;
        ...
    } else {
        // Here, it can be RssLargeViewHolder or HighlightViewHolder
        RssLargeViewHolder itemHolder = (RssLargeViewHolder) viewHolder;
        ...
    }
}

因此,正确的方法是在投射之前寻找合适的实例:

protected void doBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    if (viewHolder instanceof RssViewHolder){
        RssViewHolder itemHolder = (RssViewHolder) viewHolder;
        ...
    } else if (viewHolder instanceof RssLargeViewHolder) { 
        RssLargeViewHolder itemHolder = (RssLargeViewHolder) viewHolder;
        ...
    } else if (viewHolder instanceof HighlightViewHolder) {
        HighlightViewHolder itemHolder = (HighlightViewHolder) viewHolder;
        ...
    } else {
        // Log.e ("LOG_TAG", "Error.. Wrong type received");
    }
}

答案 1 :(得分:1)

您收到此错误是因为您试图将

中的HighlightViewHolder强制转换为RssLargeViewHolder
@Override
protected void doBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
    if (viewHolder instanceof RssViewHolder){

        RssViewHolder holder = (RssViewHolder) viewHolder;

        holder.listTitle.setText(objects.get(position).getTitle());
        holder.listPubdate.setText(objects.get(position).getPubdate());
        holder.listThumb.setImageDrawable(null);
        String thumburl = objects.get(position).getThumburl();
        loadImageIntoView(thumburl, holder.listThumb);
        setOnClickListener(holder.itemView, position);

    } else {

        RssLargeViewHolder itemHolder = (RssLargeViewHolder) viewHolder; //<--here

        itemHolder.headlineView.setText(objects.get(position).getTitle());
        itemHolder.reportedDateView.setText(objects.get(position).getPubdate());
        itemHolder.imageView.setImageBitmap(null);
        String thumburl = objects.get(position).getThumburl();
        loadImageIntoView(thumburl, itemHolder.imageView);
        setOnClickListener(itemHolder.itemView, position);

    }
}

在标记的位置上方。

希望这会有所帮助!