我得到:
尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'上的null 对象引用
此错误是崩溃的开始。 这是我第一次遇到此类错误
public class Messageadapter extends RecyclerView.Adapter {
private static final int MSG_TYPE_LEFT=0;
private static final int MSG_TYPE_RIGHT=1;
private Context mContext;
private List<Chat> mChat;
private String imageurl;
FirebaseUser fuser;
public Messageadapter(Context mContext, List<Chat> mChat,String imageurl) {
this.mContext=mContext;
this.mChat=mChat;
this.imageurl=imageurl;
}
@NonNull
@Override
public Messageadapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType==MSG_TYPE_RIGHT){
View view= LayoutInflater.from(mContext).inflate(R.layout.chat_item_right,parent,false);
return new Messageadapter.ViewHolder(view);
}else {
View view= LayoutInflater.from(mContext).inflate(R.layout.chat_item_left,parent,false);
return new Messageadapter.ViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Chat chat=mChat.get(position);
((ViewHolder) holder).show_message.setText(chat.getMessage());///////Error is said on this line/////////////////
((UserAdapter.ViewHolder) holder).profile_image.setImageResource(R.mipmap.ic_launcher);
}
@Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message;
public ImageView profile_image;
public ViewHolder(@NonNull View itemView) {
super(itemView);
show_message=itemView.findViewById(R.id.show_message);
profile_image=itemView.findViewById(R.id.profileimage);
}
}
@Override
public int getItemViewType(int position) {
fuser= FirebaseAuth.getInstance().getCurrentUser();
if(mChat.get(position).getSender().equals(fuser.getUid())){
return MSG_TYPE_RIGHT;
}
else {
return MSG_TYPE_LEFT;
}
}
}
我希望此Viewholder
选择此消息,但未显示。
答案 0 :(得分:0)
此处(((ViewHolder)持有人).show_message.setText(chat.getMessage()); 您正在投放基本回收站视图持有人,而不是您的视图持有人
改为使其像这样((UserAdapter.ViewHolder).show_message.setText(chat.getMessage());