我想在毕加索展示照片时收到NPE,
请注意,我还有另一个使用毕加索的适配器,
但是在我的新适配器中,发生了布尔值的npe错误,
和我的朋友们使用了这段代码,效果很好,
这是错误说明的代码
public boolean isVideoNews() {
return !image.isEmpty() && !video.isEmpty() ;
}
和我的适配器中与此代码相关的部分
videoIndicator.setVisibility(news.isVideoNews() ? View.VISIBLE : View.GONE);
我的适配器
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
private List<News> newsList;
public NewsAdapter(List<News> newsList) {
this.newsList = newsList;
}
@Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NewsViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false));
}
@Override
public void onBindViewHolder(NewsViewHolder holder, int position) {
holder.bindNews(newsList.get(position));
}
@Override
public int getItemCount() {
return newsList.size();
}
class NewsViewHolder extends RecyclerView.ViewHolder {
private ImageView newsImageView;
private TextView titleTextView;
private TextView dateTextView;
private View videoIndicator;
public NewsViewHolder(View itemView) {
super(itemView);
newsImageView = itemView.findViewById(R.id.iv_news_image);
videoIndicator = itemView.findViewById(R.id.iv_news_VideoIndicator);
titleTextView = itemView.findViewById(R.id.tv_news_title);
dateTextView = itemView.findViewById(R.id.tv_news_date);
}
public void bindNews(News news) {
Picasso.get().load(news.getImage()).into(newsImageView);
videoIndicator.setVisibility(news.isVideoNews() ? View.VISIBLE : View.GONE);
titleTextView.setText(news.getTitle());
dateTextView.setText(news.getDate());
}
}
}
这是我的数据模型
public class News {
private int id;
private String title;
private String content;
private String date;
private String image;
private String video;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public boolean isVideoNews() {
return !image.isEmpty() && !video.isEmpty() ;
}
}
错误
FATAL EXCEPTION: main
Process: com.example.melal.newsapp, PID: 5958
java.lang.IllegalArgumentException: Path must not be empty.
at com.squareup.picasso.Picasso.load(Picasso.java:332)
at com.example.melal.newsapp.home.NewsAdapter$NewsViewHolder.bindNews(NewsAdapter.java:53)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:31)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:17)
FATAL EXCEPTION: main
Process: com.example.melal.newsapp, PID: 9523
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
at com.example.melal.newsapp.data.News.isVideoNews(News.java:60)
at com.example.melal.newsapp.home.NewsAdapter$NewsViewHolder.bindNews(NewsAdapter.java:53)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:30)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:16)
我在调试时得到了所有数据,文本和标题以及所有内容都显示在UI上,但是我的图片没有显示,横幅图片显示在UI上的另一个适配器中,但是主要问题是我告诉过的适配器,
请帮我
最佳答案
您必须在此处检查Image路径的空值和空值。
在以下情况下放入以下行:
if(TextUtils.isEmpty(news.getImage())) {
// Load default image
newsImageView.setImageResource(R.drawable.placeholder);
} else {
Picasso.get().load(news.getImage()).into(newsImageView);
}
希望它能工作。
关于android - picasso 未在Android Studio中加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54721959/