毕加索抛出异常:context == null

picasso throw an exception: context == null

我使用它将图像加载到 RecyclerView 的视图中,但抛出异常:context == null,它发生在我获得 androidX 时。在我更新之前它工作正常; 感谢帮助^.^

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)

这是 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);

}

这是 ViewHolder 代码:

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

}

}

在我更新到 androidX 之前它工作正常。 再次感谢您的帮助^.^

这个问题基于毕加索的 github issues/1929

您不能在视图持有者中使用 Picasso.get()。基本上这是一个错误,必须修复,但现在您可以在适配器的构造函数中 post Picasso。

我也用androidX,但是因为我用dagger来提供picasso。不要面对这个问题

请将以下代码放入应用程序class。如果Android版本是7.0以上,需要添加以下代码。

Picasso.setSingletonInstance(new Picasso.Builder(this).build());

从 Picasso 库中的版本 2.71828 初始化单例实例

 //lib
 implementation 'com.squareup.picasso:picasso:2.71828'


private static boolean initializedPicasso = false;

public static void initPicasso(Context context) {
    Log.i("TAG", "initializedPicasso:" + initializedPicasso);
    if (initializedPicasso) {
        return;
    }
    try {
        Picasso.setSingletonInstance(new Picasso.Builder(context).build());
    } catch (Exception e) {
        Log.e("TAG", "Error:" + e.toString());
    }

    initializedPicasso = true;
}

您可以简单地将下面的代码块放入 setUp() 方法 Test class.

 @Before
fun setUp() {
    mockkStatic(Picasso::class)
    val picassoMock = mockkClass(Picasso::class, relaxed = true)
    coEvery {
        Picasso.get()
    } returns picassoMock
}

在您的应用程序中放入以下代码class

public class MainApplicationClass extends Application {
        
          @Override
            public void onCreate() {
                super.onCreate();
        
              // You need to add the below code:
               Picasso.setSingletonInstance(new Picasso.Builder(this).build());
             }
     }