英文:
Null pointer exception trying to invoke onClick method from custom Adapter
问题
我在Android Studio中尝试编写自己的适配器类,代码看起来像这样:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
private List<Article> articles;
private Context context;
private OnItemClickListener onItemClickListener;
public Adapter(List<Article> articles, Context context) {
this.articles = articles;
this.context = context;
}
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_ad, 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.decs);
author = itemView.findViewById(R.id.author);
published_ad = itemView.findViewById(R.id.publishedAt);
source = itemView.findViewById(R.id.source);
time = itemView.findViewById(R.id.time);
imageView = itemView.findViewById(R.id.img);
progressBar = itemView.findViewById(R.id.progress_load_photo);
this.onItemClickListener = onItemClickListener;
}
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
我使用适配器的类看起来像这样:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(false);
adapter = new Adapter(articles, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
LoadJson();
return view;
}
我遇到了一个空指针异常的错误:"尝试调用接口方法 'void com.example.app2.Adapter$OnItemClickListener.onItemClick(android.view.View, int)' 时出现空对象引用"。
英文:
I was trying to write own adapter class in Android Studio which looks something like this:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
private List<Article> articles;
private Context context;
private OnItemClickListener onItemClickListener;
public Adapter(List<Article> articles, Context context) {
this.articles = articles;
this.context = context;
}
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_ad, 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.decs);
author = itemView.findViewById(R.id.author);
published_ad = itemView.findViewById(R.id.publishedAt);
source = itemView.findViewById(R.id.source);
time = itemView.findViewById(R.id.time);
imageView = itemView.findViewById(R.id.img);
progressBar = itemView.findViewById(R.id.progress_load_photo);
this.onItemClickListener = onItemClickListener;
}
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
And the class in which I use my Adapter looks like this:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(false);
adapter = new Adapter(articles, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
LoadJson();
return view;
}
I am getting an error about null pointer exception: "Attempt to invoke interface method 'void com.example.app2.Adapter$OnItemClickListener.onItemClick(android.view.View, int)' on a null object reference"
答案1
得分: 0
你的字段 Adapter.onItemClickListener
在设置之前是空的(在你的实际代码中从未设置过)
因此,在尝试通知监听器之前,你应该检查是否有一个监听器。
@Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(v, getAdapterPosition());
}
我猜你会在你的 onCreateView
方法中使用它
adapter = new Adapter(articles, getActivity().getApplicationContext());
adapter.setOnItemClickListener(() -> { System.out.println("Click!"); });
recyclerView.setAdapter(adapter);
英文:
Your field Adapter.onItemClickListener
is null until you set it up (what you never do in your actual code)
So you should test if there is a listener before trying to notify it.
@Override
public void onClick(View v) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(v, getAdapterPosition());
}
And I guess you would use it in your onCreateView
method
adapter = new Adapter(articles, getActivity().getApplicationContext());
adapter.setOnItemClickListener(() -> { System.out.println("Click !") });
recyclerView.setAdapter(adapter);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论