在 myAdapter 中从 Context 扩展布局时获取空引用

Get a null reference while inflating a layout fromContext in myAdapter

应用程序不断崩溃,我不断收到以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
    at android.view.LayoutInflater.from(LayoutInflater.java:229)
    at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:37)
    at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:19)

这是我的代码:

public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {

    private Context mContext;
    private List<Popular> mPopular;

    public PopularAdapter(Context context, List<Popular> populars){
        this.mContext = context;
        mPopular = populars;
    }

    public PopularAdapter(List<Popular> mPopular) {
        this.mPopular = mPopular;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.popular_items,viewGroup, false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
        Popular popularCur=mPopular.get(i);
        imageViewHolder.prod_name.setText(popularCur.getProduct_title());
        imageViewHolder.prod_price.setText(popularCur.getProduct_price());
        Picasso.get()
                .load(popularCur.getProduct_image())
                .placeholder(R.drawable.img_placeholder)
                .fit()
                .centerCrop()
                .into( imageViewHolder.prod_img);
    }

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

    class ImageViewHolder extends RecyclerView.ViewHolder {
         TextView prod_name, prod_price;
         ImageView prod_img;

         ImageViewHolder(@NonNull View itemview){
            super(itemview);
            prod_name = itemview.findViewById(R.id.prodName);
            prod_price = itemview.findViewById(R.id.prodPrice);
            prod_img = itemview.findViewById(R.id.prodImage);
        }
    }
}

拜托,我真的需要帮助来理解我做错了什么,而且我是 android 的初学者。在我看来,Context 在代码中的某个时刻已被取消。我似乎不知道在哪里以及如何尝试使用片段让我有点困惑。

你的代码是正确的,我建议你检查你的 activity 并查看你在哪里创建适配器并在那里处理 android 的生命周期,也许 SO 正在杀死你的 activity 并且当 activity 再次重新创建适配器的实例时丢失上下文。

请问post activity 的代码对

有帮助吗
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.view.LayoutInflater.from(LayoutInflater.java:229)
        at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:37)
        at com.example.cnatra_measure.Adapters.PopularAdapter.onCreateViewHolder(PopularAdapter.java:19)

以上错误告诉您您正在尝试使用 null Context 来扩充您的适配器项布局。

这是因为你有两个构造函数:一个有上下文,另一个没有。

// Here you're passing the context to the adapter
public PopularAdapter(Context context, List<Popular> populars){
    ...
}

// No context here.
public PopularAdapter(List<Popular> mPopular) {
    ...
}

而且您似乎调用了 no context 构造函数。因此上下文为空。

您可以通过在内部使用适配器 ViewGroup 而不是从调用者传递上下文来避免此问题。您可以将适配器更新为如下内容:

public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.ImageViewHolder> {

    private List<Popular> mPopular;

    // Use only one constructor.
    public PopularAdapter(List<Popular> mPopular) {
        this.mPopular = mPopular;
    }


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

      // Use the viewGroup i.e parent of the Adapter.
      Context context = viewGroup.getContext();
      LayoutInflater inflater = LayoutInflater.from(context);

      // Inflate the layout
      View v = inflater.inflate(R.layout.popular_items, viewGroup, false);
      return new ImageViewHolder(v);
    }

    ...

}