空指针异常,尝试从自定义适配器调用 onClick 方法。

huangapple go评论113阅读模式
英文:

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&lt;Adapter.MyViewHolder&gt;{
private List&lt;Article&gt; articles;
private Context context;
private OnItemClickListener onItemClickListener;
public Adapter(List&lt;Article&gt; 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 方法中使用它 空指针异常,尝试从自定义适配器调用 onClick 方法。

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 空指针异常,尝试从自定义适配器调用 onClick 方法。

adapter = new Adapter(articles, getActivity().getApplicationContext());
adapter.setOnItemClickListener(() -&gt; { System.out.println(&quot;Click !&quot;) });
recyclerView.setAdapter(adapter);

huangapple
  • 本文由 发表于 2020年5月29日 20:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/62085750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定