2

What I'm trying to do is to display images using an image url and I'm using the glide library to load these images however whenever I try to run the application, the images are unable to show or wont load on the activity. I'm able to see the text being displayed but not the images

This is the Error I'm getting

java.lang.IllegalArgumentException: You cannot start a load on a null Context
11-11 03:23:01.713 5428-5428/? W/System.err:     at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:84)
11-11 03:23:01.713 5428-5428/? W/System.err:     at com.bumptech.glide.Glide.with(Glide.java:629)

This is the code

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

    private List<SampleModel> list;
    private Context mContext = null;

    public SampleAdapter(List<SampleModel> sampleModelList) {
        this.list = sampleModelList;
    }

    @Override
    public SampleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WatchlistAdapter.ViewHolder holder, int position) {

       String image = "https://image.tmdb.org/t/p/w500/9HE9xiNMEFJnCzndlkWD7oPfAOx.jpg";

        try{
               final SampleModel sample = list.get(holder.getAdapterPosition());

               if(sample != null) {
              holder.title.setText(sample.getTitle());
              Glide.with(mContext).load(image)
                   .placeholder(R.drawable.placeholder)
                   .dontAnimate()
                   .fitCenter()
                   .crossFade()
                   .diskCacheStrategy(DiskCacheStrategy.ALL)
                   .into(holder.cover);
            } else {
              Glide.clear(holder.cover);
            }
        } catch(Exception e) {
             e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
       return (list != null? list.size():0);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ImageView cover;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            cover= (ImageView) itemView.findViewById(R.id.coverImage);
        }
  }
2
  • 1
    Check your mContext. Probably you didn't initialize the context properly and hence it's null which is causing the problem. Commented Nov 10, 2016 at 19:36
  • @oathkeeper I've updated my code. please check Commented Nov 10, 2016 at 19:45

3 Answers 3

1

do this :

public SampleAdapter(List<SampleModel> sampleModelList, Context context) {
    this.list = sampleModelList;
    mContext = context ;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Before this :

 Glide.with(mContext).load(image)
               .placeholder(R.drawable.placeholder)
               .dontAnimate()
               .fitCenter()
               .crossFade()
               .diskCacheStrategy(DiskCacheStrategy.ALL)
               .into(holder.cover);

mContext is not assigned any Context value. It still has the value you have initialised with (that is null). So, you need to assign its value as suggested by @Kapta and pass the context as parameter in the SampleAdapter constructor.

Comments

0

Actually as long as your ImageView holder.cover is not null, you may just conveniently use holder.cover.getContext() for that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.