picasso throw an exception: context == null

i use it load image into view at RecyclerView, but throws an exception : context == null, and its occured when i got androidX. its working fine before i update; thanks for helping ^.^

java.lang.IllegalStateException: context == null
    at com.squareup.picasso.Picasso.get(Picasso.java:681)
    at com.android.mytestdemo.presenter.NewsInfoViewHolder.setData(NewsInfoViewHolder.java:67)
    at com.android.mytestdemo.presenter.NewsInfoViewHolder.setData(NewsInfoViewHolder.java:14)
    at com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter.OnBindViewHolder(RecyclerArrayAdapter.java:627)
    at com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter.onBindViewHolder(RecyclerArrayAdapter.java:622)
    at com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter.onBindViewHolder(RecyclerArrayAdapter.java:50)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at com.android.mytestdemo.adapter.NewsInfoAdapter.onBindViewHolder(NewsInfoAdapter.java:32)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)

This is newsInfoAdapter:

public class NewsInfoAdapter extends RecyclerArrayAdapter {

private Context mContext;

public NewsInfoAdapter(Context context) {
    super(context);
    this.mContext = context;
}

@Override
public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

    return new NewsInfoViewHolder(parent,mContext);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
    super.onBindViewHolder(holder, position, payloads);
    holder.itemView.setTag(position);

}

This is ViewHolder code :

public class NewsInfoViewHolder extends BaseViewHolder<NewsInfo> {

private Context mContext;

private ImageView imageView;
private TextView tvTitle;
private ImageView ivPlayAndPause;

private boolean isPlaying = false;

public NewsInfoViewHolder(ViewGroup parent, Context context) {
    super(parent, R.layout.easy_rv_item);

    this.mContext = context;

    imageView = $(R.id.news_img);
    ivPlayAndPause = $(R.id.iv_play);
    tvTitle = $(R.id.news_title);


    ivPlayAndPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (isPlaying){
                ivPlayAndPause.setBackground(mContext.getResources().getDrawable(R.drawable.icon_play_48dp));
                isPlaying = false;
            }else {
                ivPlayAndPause.setBackground(mContext.getResources().getDrawable(R.drawable.icon_pause_48dp));
                isPlaying = true;
            }
        }
    });

}

@Override
public void setData(NewsInfo data) {
    super.setData(data);
    String picTo ;
    String pic ;

    if (data.getImgUrlPicTo() != null && !(data.getImgUrlPicTo().equals(""))){


        if (data.getImgUrlPicTo().contains("https:"))
            picTo = data.getImgUrlPicTo();
        else
            picTo = "https:" + data.getImgUrlPicTo();

        Picasso.get().load(picTo).fit().into(imageView);

    }else if (data.getImgUrlPic() != null && !(data.getImgUrlPic().equals(""))){

        if (data.getImgUrlPic().contains("https:"))
            pic = data.getImgUrlPic();
        else
            pic = "https:" + data.getImgUrlPic();


        Picasso.get().load(pic).fit().into(imageView);
    }

    tvTitle.setText(data.getTitle());

}

}

its working fine before i update to androidX. thanks for help again ^.^

Answer

Base on this issue on picasso’s github issues/1929

You can’t use Picasso.get() in view holder. Basically it’s a bug and must be fixed but for now you can post Picasso in constructor of your adapter.

I use androidX too, but because i use dagger to provide picasso . don’t face with this problem

Leave a Reply

Your email address will not be published. Required fields are marked *