ImageView.setBackgroundResource
ImageView.setBackgroundResource
Attempt to invoke virtual method 'void
android.widget.ImageView.setBackgroundResource(int)' on a null object
reference
at com.Adapter.MessageAdapter.onBindViewHolder(MessageAdapter.java:240)
at com.Adapter.MessageAdapter.onBindViewHolder(MessageAdapter.java:37)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7093)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7172)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6037)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6304)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6143)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6139)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView read_chat;
}
onBindViewHolder
public void onBindViewHolder(@NonNull MessageAdapter.ViewHolder holder, int position) {
int type = holder.getItemViewType();
Chat chat = mChat.get(position);
if (chat.isIsseen()) {
holder.read_chat. setBackgroundResource(R.drawable.ic_check_double_fill_read);
} else {
holder.read_chat. setBackgroundResource(R.drawable.ic_check_double_fill);
}
}
ViewHolder 视图
public ViewHolder(View itemView) {
super(itemView);
read_chat = itemView.findViewById(R.id.read_chat);
}
我不确定你在 onCreateViewHolder 函数中做了什么,但通常当我看到这个时,是因为你夸大了错误的布局。确保你正在膨胀你的 list_item xml 布局。希望这有帮助。
试试这个
imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_selected_image));
Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference at com.Adapter.MessageAdapter.onBindViewHolder(MessageAdapter.java:240) at com.Adapter.MessageAdapter.onBindViewHolder(MessageAdapter.java:37) at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7093) at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7172) at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6037) at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6304) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6143) at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6139) at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView read_chat;
}
onBindViewHolder
public void onBindViewHolder(@NonNull MessageAdapter.ViewHolder holder, int position) {
int type = holder.getItemViewType();
Chat chat = mChat.get(position);
if (chat.isIsseen()) {
holder.read_chat. setBackgroundResource(R.drawable.ic_check_double_fill_read);
} else {
holder.read_chat. setBackgroundResource(R.drawable.ic_check_double_fill);
}
}
ViewHolder 视图
public ViewHolder(View itemView) {
super(itemView);
read_chat = itemView.findViewById(R.id.read_chat);
}
我不确定你在 onCreateViewHolder 函数中做了什么,但通常当我看到这个时,是因为你夸大了错误的布局。确保你正在膨胀你的 list_item xml 布局。希望这有帮助。
试试这个
imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_selected_image));