为什么在使用翻新库时会出现NullPointerException

时间:2019-03-25 06:20:44

标签: android-recyclerview

我正在使用翻新库来接收数据并在RecyclerView上使用它,但是在调用recyclerView时我遇到了null对象引用上的NullPointerExceptionin的问题,此后阅读了Stackoverflow上的问题,但它们没有帮助我,我是确保我从服务器接收到数据,但不知道为什么会出现null错误,将不胜感激

错误:

  java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at Adaptor.NewsAdaptor.getItemCount(NewsAdaptor.java:50)

MainActivity

public class TextActivity extends Activity {

RecyclerView recyclerView;
List<NewsItem> listofNews;
NewsAdaptor newsAdaptor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text);

    //  Must Instanciate the Requirements
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(TextActivity.this));

    listofNews = new ArrayList<>();

    Call<FetechingNewsResponse> call = RetrofitClient.getInstance().getApi().getTrafficeNews();

    call.enqueue(new Callback<FetechingNewsResponse>() {
        @Override
        public void onResponse(Call<FetechingNewsResponse> call, Response<FetechingNewsResponse> response) {


            Log.v("fromTextActivity", response.body().isStatus() + "");
            listofNews = response.body().getList();

            newsAdaptor = new NewsAdaptor(TextActivity.this, listofNews);
            recyclerView.setAdapter(newsAdaptor);
        }

        @Override
        public void onFailure(Call<FetechingNewsResponse> call, Throwable t) {

        }
    });

}

}

NewsAdaptor类

 public class NewsAdaptor extends     
      RecyclerView.Adapter<NewsAdaptor.NewsViewHolder> {

private Context mct;
private List<NewsItem> newsItems;

public NewsAdaptor(Context mct, List<NewsItem> newsItems) {
    this.mct = mct;
    this.newsItems = new ArrayList<>();
    this.newsItems = newsItems;
}

@NonNull
@Override
public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View view = LayoutInflater.from(mct).inflate(R.layout.recycle_item, viewGroup, false);
    return new NewsViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull NewsViewHolder newsViewHolder, int i) {

    NewsItem newsI = newsItems.get(i);

    Picasso.get().load(newsI.getThumb()).into(newsViewHolder.newsPhoto);
    newsViewHolder.newsMainBody.setText(newsI.getBody());
    newsViewHolder.newsTitle.setText(newsI.getTitle());
}

@Override
public int getItemCount() {
    Log.v("from_adoptor", newsItems.size() + "");
    return this.newsItems.size();
}

class NewsViewHolder extends RecyclerView.ViewHolder {

    TextView newsMainBody, newsTitle;
    ImageView newsPhoto;

    public NewsViewHolder(@NonNull View itemView) {
        super(itemView);
        newsMainBody = itemView.findViewById(R.id.display_body);
        newsTitle = itemView.findViewById(R.id.display_text);
        newsPhoto = itemView.findViewById(R.id.display_photo);
    }
}

}

ّ FetchingClass

public class FetechingNewsResponse {

private boolean status;
private List<NewsItem> list;

public FetechingNewsResponse(boolean status, String news, NewsItem newsItem, List<NewsItem> list) {
    this.status = status;
    this.list = list;
}

public boolean isStatus() {
    return status;
}

public List<NewsItem> getList() {
    return list;
}

}

Api

公共接口Api {

 @GET("news")
Call<FetechingNewsResponse> getTrafficeNews();

}

0 个答案:

没有答案