gpt4 book ai didi

android - ClassCastException 自定义 ViewHolder 无法转换为另一个 Viewholder

转载 作者:行者123 更新时间:2023-12-04 01:43:43 27 4
gpt4 key购买 nike

我的项目应该基于用户输入(用户发送文本、拍照或录制视频)创建多个 View ,类似于 WhatsApp 聊天 Activity ,结构几乎相同。适配器应该能够通过使用 getItemViewType() 来识别项目的 View 类型。如果尝试使用视频录制功能,它可以工作,但如果我尝试拍照或发送文本,则应用程序崩溃并出现以下错误:

java.lang.ClassCastException:AppPackage.myAdapter$TextViewHolder 无法转换为 AppPackage.myAdapter$ImageViewHolder

从输入中获取的所有数据都添加到列表中,然后传递给 onBindViewHolder

已经尝试将 onBindViewHolder(CustomViewHolder myViewHolder, int position) 更改为 onBindViewHolder(RecyclerView.ViewHolder, int position)
并在开关的每个“案例”中添加中断。还是行不通

适配器

public class myAdapter extends   
RecyclerView.Adapter<RecyclerView.ViewHolder> {

List<ModelloDati> lista;
Context context;


public class ImageViewHolder extends RecyclerView.ViewHolder {
//ImageView mImage;
private ImageView imageView;
public ImageViewHolder(View itemView) {
super (itemView);
imageView = itemView.findViewById(R.id.immagine);
}
}

public class TextViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private TextView titolo;

public TextViewHolder(View v) {
super(v);
titolo = itemView.findViewById(R.id.testo);
}
}

public class VideoViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
private VideoView videoView;

public VideoViewHolder(View v) {
super(v);
videoView = itemView.findViewById(R.id.rec);
}
}

public myAdapter(List<ModelloDati> lista,Context context) {
this.lista = lista;
this.context = context;

}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
if (viewType == 0) {
// create a new view
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_foto,
parent, false);
// set the view's size, margins, paddings and layout parameters
ImageViewHolder vh = new ImageViewHolder(v);
return vh;
}
if (viewType == 2) {
// create a new view
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_video,
parent, false);
// set the view's size, margins, paddings and layout parameters
VideoViewHolder vh = new VideoViewHolder(v);
return vh;
} else {
// create a new view
View v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_testo,
parent, false);
// set the view's size, margins, paddings and layout parameters
TextViewHolder vh = new TextViewHolder(v);
return vh;
}
}


@Override
public int getItemViewType(int position){

switch (lista.get(position).getTYPE()) {
case 0: return 0;
case 1: return 1;
case 2: return 2;
}
return 0;

}


@Override
public int getItemCount() {
if (lista == null)
return 0;
return lista.size();
}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder myViewHolder, int
position) {

ModelloDati oggetto = lista.get(position);

switch (myViewHolder.getItemViewType()) {

case 0:
TextViewHolder viewHolder = (TextViewHolder) myViewHolder;
viewHolder.titolo.setText(oggetto.getTesto());
break;

case 1:
ImageViewHolder imageViewHolder = (ImageViewHolder)
myViewHolder;

imageViewHolder.imageView.setImageBitmap(oggetto.getImmagini());
imageViewHolder.imageView.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

break;


case 2:
VideoViewHolder videoViewHolder = (VideoViewHolder)
myViewHolder;
videoViewHolder.videoView.setVideoURI(oggetto.getUri());
MediaController mediaController = new
MediaController(context);
mediaController.setAnchorView(videoViewHolder.videoView);

videoViewHolder.videoView.setMediaController(mediaController);
videoViewHolder.videoView.requestFocus();
videoViewHolder.videoView.start();

videoViewHolder.videoView.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
break;

default:
viewHolder = (TextViewHolder) myViewHolder;
viewHolder.titolo.setText("ERRORE");


}

}


}

日志猫
2019-05-21 10:24:18.728 11089-11089/com.sinfo.chat E/AndroidRuntime: FATAL  
EXCEPTION: main
Process: com.sinfo.chat, PID: 11089
java.lang.ClassCastException: com.sinfo.chat.myAdapter$TextViewHolder cannot be
cast to com.sinfo.chat.myAdapter$ImageViewHolder
at com.sinfo.chat.myAdapter.onBindViewHolder(myAdapter.java:119)
at

android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:587)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:23169)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23169)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
at android.view.View.measure(View.java:23169)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2725)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1575)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1858)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1463)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7190)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

最佳答案

ImageViewHolder使用类型 0 创建在 onCreateViewHolder并以 1 类型类型转换在 onBindViewHolder .使用常量而不是 float 魔数(Magic Number)

关于android - ClassCastException 自定义 ViewHolder 无法转换为另一个 Viewholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com