我还是Android编程的新手,我一直在尝试制作一个简单的应用程序进行练习。我想获取API新闻,并且我的代码抛出NullPointerException。这是代码
NewsAdapter.java
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {
private List<Article> articles;
private Context context;
private OnItemClickListener onItemClickListener;
public NewsAdapter(List<Article> articles, Context context) {
this.articles = articles;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(view, onItemClickListener);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holders, int position) {
final MyViewHolder holder2 = holders;
Article model = articles.get(position);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(Utils.getRandomDrawbleColor());
requestOptions.error(Utils.getRandomDrawbleColor());
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.centerCrop();
Glide.with(context)
.load(model.getUrlToImage())
.apply(requestOptions)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder2.progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder2.progressBar.setVisibility(View.GONE);
return false;
}
})
.transition(DrawableTransitionOptions.withCrossFade())
.into(holder2.imageView);
holder2.title.setText(model.getTitle());
holder2.desc.setText(model.getDescription());
holder2.source.setText(model.getSource().getName());
holder2.time.setText(" \u2022 "+Utils.DateToTimeFormat(model.getPublishedAt()));
holder2.published_at.setText(Utils.DateFormat(model.getPublishedAt()));
holder2.author.setText(model.getAuthor());
}
@Override
public int getItemCount() {
return articles.size();
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onItemClick(View view, int position);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title, desc, author, published_at, source, time;
ImageView imageView;
ProgressBar progressBar;
OnItemClickListener onItemClickListener;
public MyViewHolder(View itemView, OnItemClickListener onItemClickListener) {
super(itemView);
itemView.setOnClickListener(this);
title = itemView.findViewById(R.id.title);
desc = itemView.findViewById(R.id.desc);
author = itemView.findViewById(R.id.author);
published_at = itemView.findViewById(R.id.publishedAt);
source = itemView.findViewById(R.id.source);
time = itemView.findViewById(R.id.time);
imageView = itemView.findViewById(R.id.img_news);
progressBar = itemView.findViewById(R.id.load_photo);
this.onItemClickListener = onItemClickListener;
}
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
这是代码Utils.java
public class Utils {
public static ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#ffeead")),
new ColorDrawable(Color.parseColor("#93cfb3")),
new ColorDrawable(Color.parseColor("#fd7a7a")),
new ColorDrawable(Color.parseColor("#faca5f")),
new ColorDrawable(Color.parseColor("#1ba798")),
new ColorDrawable(Color.parseColor("#6aa9ae")),
new ColorDrawable(Color.parseColor("#ffbf27")),
new ColorDrawable(Color.parseColor("#d93947"))
};
public static ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
public static String DateToTimeFormat(String oldstringDate){
PrettyTime p = new PrettyTime(new Locale(getCountry()));
String isTime = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
Locale.ENGLISH);
Date date = sdf.parse(oldstringDate);
isTime = p.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return isTime;
}
public static String DateFormat(String oldstringDate){
String newDate;
SimpleDateFormat dateFormat = new SimpleDateFormat("E, d MMM yyyy", new Locale(getCountry()));
try {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(oldstringDate);
newDate = dateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
newDate = oldstringDate;
}
return newDate;
}
public static String getCountry(){
Locale locale = Locale.getDefault();
String country = String.valueOf(locale.getCountry());
return country.toLowerCase();
}
在此代码中,我得到
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.example.android.likeeatapplication.Utils.DateToTimeFormat(Utils.java:39)
at com.example.android.likeeatapplication.Adapter.NewsAdapter.onBindViewHolder(NewsAdapter.java:81)
at com.example.android.likeeatapplication.Adapter.NewsAdapter.onBindViewHolder(NewsAdapter.java:29)
那我该如何解决呢?