嗨,我正在开发android,但启动后却遇到了以下异常 ava.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()' 在edgar.yodgorbek.sportnews.sportactivities.BBCSportFragment $ 1.onResponse(BBCSportFragment.java:69)
在BBCSportFragment类之下
public class BBCSportFragment extends Fragment implements ArticleAdapter.ClickListener {
public List<Article> articleList = new ArrayList<>();
@ActivityContext
public Context activityContext;
@ApplicationContext
public Context mContext;
@BindView(R.id.recycler_view)
RecyclerView recyclerView;
BBCSportFragmentComponent bbcSportFragmentComponent;
BBCFragmentContextModule bbcFragmentContextModule;
private SportNews sportNews;
private ArticleAdapter articleAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bbcsport, container, false);
ButterKnife.bind(this, view);
SportInterface sportInterface = SportClient.getApiService();
Call<SportNews> call = sportInterface.getArticles();
call.enqueue(new Callback<SportNews>() {
@Override
public void onResponse(Call<SportNews> call, Response<SportNews> response) {
sportNews = response.body();
if (sportNews != null && sportNews.getArticles() != null) {
articleList.addAll(sportNews.getArticles());
}
articleAdapter = new ArticleAdapter(articleList, sportNews);
ApplicationComponent applicationComponent = MyApplication.get((Activity) getContext()).getApplicationComponent();
bbcSportFragmentComponent = (BBCSportFragmentComponent) DaggerApplicationComponent.builder().contextModule(new ContextModule(getContext())).build();
bbcSportFragmentComponent.injectBBCSportFragment(BBCSportFragment.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(articleAdapter);
}
@Override
public void onFailure(Call<SportNews> call, Throwable t) {
}
});
return view;
}
}
ArticleAdapter下面
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.CustomViewHolder> {
public static final String urlKey = "urlKey";
public static final String imageKey = "imageKey";
public ArticleAdapter.ClickListener listener;
Context context;
private List<Article> articles = new ArrayList<Article>();
public ArticleAdapter(List<Article> articles, SportNews sportNews) {
this.articles = articles;
this.listener = listener;
}
public ArticleAdapter(ArticleAdapter.ClickListener clickListener) {
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.article_list, null);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int position) {
Article article = articles.get(position);
customViewHolder.articleAuthor.setText(article.getAuthor());
customViewHolder.articleTitle.setText(article.getTitle());
Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);
customViewHolder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(v.getContext(), DetailActivity.class);
intent.putExtra("urlKey", article.getUrl());
intent.putExtra("imageKey", article.getUrlToImage());
v.getContext().startActivity(intent);
});
}
@Override
public int getItemCount() {
if (articles == null) return 0;
return articles.size();
}
public interface ClickListener {
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.articleAuthor)
TextView articleAuthor;
@BindView(R.id.articleTitle)
TextView articleTitle;
@BindView(R.id.articleImage)
ImageView articleImage;
public CustomViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
答案 0 :(得分:0)
以这种方式分配之前先进行空检查:
@Override
public void onResponse(Call<SportNews> call, Response<SportNews> response) {
if(response!=null){
sportNews = response.body();
if (sportNews != null && sportNews.getArticles() != null) {
articleList.addAll(sportNews.getArticles());
}
articleAdapter = new ArticleAdapter(articleList, sportNews);
ApplicationComponent applicationComponent = MyApplication.get((Activity) getContext()).getApplicationComponent();
bbcSportFragmentComponent = (BBCSportFragmentComponent) DaggerApplicationComponent.builder().contextModule(new ContextModule(getContext())).build();
bbcSportFragmentComponent.injectBBCSportFragment(BBCSportFragment.this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(articleAdapter);
}}
类似地,在从对象获取任何内容之前,请检查null以免出现nullpointer异常。
@Override
public void onBindViewHolder(@NonNull CustomViewHolder customViewHolder, int position) {
Article article = articles.get(position);
if(article!=null){
customViewHolder.articleAuthor.setText(article.getAuthor());
customViewHolder.articleTitle.setText(article.getTitle());
Picasso.get().load(article.getUrlToImage()).into(customViewHolder.articleImage);}
customViewHolder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(v.getContext(), DetailActivity.class);
intent.putExtra("urlKey", article.getUrl());
intent.putExtra("imageKey", article.getUrlToImage());
v.getContext().startActivity(intent);
});
}