'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 view

public ViewHolder(View itemView) {
super(itemView);

  read_chat = itemView.findViewById(R.id.read_chat);

}


Solution 1:[1]

I'm not sure what you did in your onCreateViewHolder function, but generally when I see this it's because you inflated the wrong layout. Make sure you are inflating your list_item xml layout. Hope this helps.

Solution 2:[2]

Try this

imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_selected_image));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tomcha34
Solution 2 Kishan Viramgama