毕加索未在Android Studio中加载

时间:2019-02-16 10:09:53

标签: android picasso recycler-adapter

我想在毕加索展示照片时收到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上的另一个适配器中,但是主要问题是我告诉过的适配器,< / p>

请帮助我

2 个答案:

答案 0 :(得分:0)

您必须在此处检查图像路径的空值和空值。

在以下情况下放入以下行:

command

希望它会起作用。

答案 1 :(得分:0)

您需要提供序列化名称并公开Model类的该字段,以便您的gson可以解析它。当前您尚未将其设置为任何字段,因此GSON不会相应地解析您的响应。下面是如何在类中定义它的示例代码。您需要相应地公开所有类字段。

@SerializedName("foo")      //name inside quotes must match with your json field name.
@Expose
private String foo;