我正在使用NEWSAPI的API创建新闻应用。每当我点击新闻时它都会崩溃

时间:2017-10-20 14:44:12

标签: java android api

每当我点击新闻时它崩溃,下面是LOGCAT

java.lang.NullPointerException:尝试调用虚方法' long java.util.Date.getTime()'在null对象引用上                                                                                     在com.jimdo.saifstudios.lynknews.Adapter.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.java:105)                                                                                     在com.jimdo.saifstudios.lynknews.Adapter.ListNewsAdapter.onBindViewHolder(ListNewsAdapter.java:70)

ListNewsAdapter

class ListNewsViewHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener
{

ItemClickListener itemClickListener;

TextView article_title;
RelativeTimeTextView article_time;
CircleImageView article_image;

public ListNewsViewHolder(View itemView) {
    super(itemView);
    article_image = 
(CircleImageView)itemView.findViewById(R.id.article_image);
    article_title = (TextView) itemView.findViewById(R.id.article_title);
    article_time = (RelativeTimeTextView) 
itemView.findViewById(R.id.article_time);

    itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

public void setArticle_title(TextView article_title) {
    this.article_title = article_title;
}

public void setArticle_time(RelativeTimeTextView article_time) {
    this.article_time = article_time;
}

public void setArticle_image(CircleImageView article_image) {
    this.article_image = article_image;
}

@Override
public void onClick(View view) {
    itemClickListener.onClick(view,getAdapterPosition(),false);

}
}

public class ListNewsAdapter extends 
RecyclerView.Adapter<ListNewsViewHolder> 
{
private List<Article> articleList;
private Context context;

public ListNewsAdapter(List<Article> articleList, Context context) {
    this.articleList = articleList;
    this.context = context;
}

@Override
public ListNewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View itemView = inflater.inflate(R.layout.news_layout,parent,false);
    return new ListNewsViewHolder(itemView);
}

@Override
public void onBindViewHolder(ListNewsViewHolder holder, int position) {

    Picasso.with(context)
            .load(articleList.get(position).getUrlToImage())
            .into(holder.article_image);
    if (articleList.get(position).getTitle().length() > 65)
     holder.article_title.setText(articleList.get(position).getTitle().substring(0,65)+"...");
    else
        holder.article_title.setText(articleList.get(position).getTitle());

    Date date=null;
    try
    {
        date = ISO8601Parse.parse(articleList.get(position).getPublishedAt());
    }catch (ParseException ex)
    {
        ex.printStackTrace();
    }
    holder.article_time.setReferenceTime(date.getTime());

    // Set event click
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            Intent detail = new Intent(context,DetailArticle.class);
            detail.putExtra("webURL",articleList.get(position).getUrl());
            detail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(detail);



        }
    });

}

@Override
public int getItemCount() {
    return articleList.size();
}
}

2 个答案:

答案 0 :(得分:0)

您的日期对象为空。您正在执行date.getTime()而不检查它是否为null。将holder.article_time.setReferenceTime(date.getTime)移动到日期下方的try块中,您不应再崩溃。

然后你需要弄清楚如何解析开始日期的错误。

答案 1 :(得分:0)

你有问题

holder.article_time.setReferenceTime(date.getTime());

日期变量仍然为空它未初始化,因此您在

时遇到问题
ISO8601Parse.parse(articleList.get(position).getPublishedAt());

它抛出ParseException检查它。