如何修复“尝试在空对象引用上调用虚拟方法”

时间:2019-08-24 18:45:09

标签: android android-studio

每次我尝试运行我的应用程序时,它都会运行,但随后崩溃,并且我在logcat中收到相同的消息。奇怪的是这两行(我在代码所引用的行中添加了注释)。它指的是当它崩溃时我还没有碰过它们。这就是代码一直以来的样子,因为我继承了此应用程序的工作原理,而且一直如此。这是logcat消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.gabe.politicianspulse.NewsRvAdapter.onBindViewHolder(NewsRvAdapter.java:43)
        at com.example.gabe.politicianspulse.NewsRvAdapter.onBindViewHolder(NewsRvAdapter.java:19)
        at

public class NewsRvAdapter extends RecyclerView.Adapter<NewsRvAdapter.MyViewHolder> { //this is line 19
private Context context;
private List<NewsItem> newsList;

//default constructor
public NewsRvAdapter(){}

public NewsRvAdapter(List<NewsItem> newsList, Context context){
    this.newsList = newsList;
    this.context = context;
}
@NonNull
@Override
public NewsRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view;
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    view = layoutInflater.inflate(R.layout.news_card, viewGroup, false);
    final MyViewHolder viewHolder = new MyViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final NewsRvAdapter.MyViewHolder myViewHolder, int i) {
    myViewHolder.title.setText(newsList.get(i).getTitle());

   myViewHolder.description.setText(newsList.get(i).getDescription());//this line 43 

    myViewHolder.date.setText(newsList.get(i).getDate());

    //Load image URL using Picasso
    Picasso.get()
            .load(newsList.get(i).getImageUrl())
            .centerCrop()
            .fit()
            .into(myViewHolder.photoUrl);

    //on click listener that opens url in webview
    myViewHolder.photoUrl.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            String link = newsList.get(myViewHolder.getAdapterPosition()).getLink();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse(link));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

    //onclick that opens new activity
    myViewHolder.title.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent i = new Intent(context, SingleNews.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra("title", newsList.get(myViewHolder.getAdapterPosition()).getTitle());
            i.putExtra("urlToImage", newsList.get(myViewHolder.getAdapterPosition()).getImageUrl());
            i.putExtra("publishedAt", newsList.get(myViewHolder.getAdapterPosition()).getDate());
            i.putExtra("content", newsList.get(myViewHolder.getAdapterPosition()).getContent());
            context.startActivity(i);
        }
    });
}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title, description, date;
    ImageView photoUrl;
    LinearLayout viewContainer;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.news_title);
        date = itemView.findViewById(R.id.news_date);
        //description = itemView.findViewById(R.id.news_description);
        photoUrl = itemView.findViewById(R.id.news_photo);
        viewContainer = itemView.findViewById(R.id.newsView_container);
    }
}

}

1 个答案:

答案 0 :(得分:-1)

您评论了MyViewHolder构造函数上的描述textView初始化,因此在调用setText()时必须取消注释以避免nullPointerExeption