使用Glide加载图片网址时出错

时间:2016-11-10 19:33:44

标签: android android-glide

我尝试做的是使用图片网址显示图片,并且我使用滑动库加载这些图片但是每当我尝试运行应用程序时,图像都无法显示或不会加载活动。我能够看到正在显示的文字而不是图像

这是我遇到的错误

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)

这是代码

 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);
        }
  }

1 个答案:

答案 0 :(得分:0)

这样做:

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