我用它在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之前,它工作正常。 再次感谢您的帮助^。^
答案 0 :(得分:2)
请在应用程序类中放置以下代码。如果Android版本为7.0以上,则需要添加以下代码。
Picasso.setSingletonInstance(new Picasso.Builder(this).build());
答案 1 :(得分:1)
在 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;
}
答案 2 :(得分:0)
基于此问题,在毕加索的github issues/1929
您不能在视图持有人中使用Picasso.get()
。基本上,这是一个错误,必须加以修复,但现在您可以将Picasso张贴在适配器的构造函数中。
我也使用androidX,但是因为我使用dagger提供了毕加索。不要面对这个问题
答案 3 :(得分:0)
您可以简单地将以下代码块放入测试类的setUp()方法中。
@Before
fun setUp() {
mockkStatic(Picasso::class)
val picassoMock = mockkClass(Picasso::class, relaxed = true)
coEvery {
Picasso.get()
} returns picassoMock
}