java.lang.ClassCastException : cannot be cast to RssLargeViewHolder

标签 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,所以这就是我想出的方法,但出现了此错误。

感谢任何帮助!谢谢。

最佳答案

您的树类型为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");
    }
}

关于java.lang.ClassCastException : cannot be cast to RssLargeViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54557101/

相关文章:

android - 如何伪造屏幕分辨率

java - Realm 返回对象的空列表

android - 查找超过 2 个数字的 LCM 的工作示例

java - 编译 com.android.support :support-v4:24. 0.0 时出现 android 错误

java - 使用 Java 将 EPUB 转换为 PDF

java - 如何在程序运行时播放背景音乐?在java中

java - Font.getNumGlyphs() 返回的数字

java - Jenkins 后台生成过程 - 不会留在队列中的作业

java - 没有可行的替代输入 '\\n"Python 错误

php - android httppost 为什么在 PHP/MySQL 中使用 $_REQUEST 而不是 $_POST ?