提问人:daring 提问时间:12/20/2019 最后编辑:Steffen Harbichdaring 更新时间:12/21/2019 访问量:145
在 myAdapter 中扩充布局 fromContext 时获取 null 引用 [closed]
Get a null reference while inflating a layout fromContext in myAdapter [closed]
问:
应用程序不断崩溃,我不断收到以下错误:
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
答:
1赞
Rendón
12/20/2019
#1
您的代码是正确的,我建议您查看您的活动,看看您在哪里创建适配器并解决 android 的生命周期,也许 SO 正在杀死您的活动,当活动再次重新创建时,适配器的实例会丢失上下文。
您可以发布活动代码,这将有所帮助
1赞
ישו אוהב אותך
12/21/2019
#2
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) {
...
}
似乎你调用了构造函数。因此,上下文为 null。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);
}
...
}
评论
mContext
getSystemService(...)
LaoyutInflater.from(mContext)
viewGroup.getContext()