java - java.lang.ClassCastException:无法转换为 RssLargeViewHolder
问题描述
有人可以帮我吗?
尝试运行应用程序时出现此错误(标题)。
这是完整的错误日志:
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");
}
}
推荐阅读
- javascript - 无法使用 innerHtml 定义 HTML 标记的 html 内容
- karate - 空手道 API 测试 - 当 Content-Type 为 application/jwt 时,API POST 请求无法发送正文
- multithreading - JavaFX MediaPlayer - 更新标签以显示线程/任务中的“当前时间”
- docker - 如何设置 nginx-proxy 以选择正确的容器而不需要端口号
- remote-access - 如何在serveo上运行本地项目
- python - 如何从python目录中打开一个随机文件?
- javascript - 如何将附加字段与 FormData 中的文件相关联
- java - Spring Data Mongo @Indexed 多次创建索引
- algorithm - 检查新边是否会使 DAG 循环
- powershell - Powershell 从远程主机获取变量值