适配器抛出 java.lang.NullPointerException
Posted
技术标签:
【中文标题】适配器抛出 java.lang.NullPointerException【英文标题】:Adapter is throwing a java.lang.NullPointerException 【发布时间】:2020-03-07 08:34:57 【问题描述】:我收到以下错误:
java.lang.NullPointerException:尝试调用接口方法 空对象引用上的“java.lang.Object java.util.List.get(int)” 在 com.example.steblinatetiana.lesson02.SportAdapter.onBindViewHolder(SportAdapter.java:66) 在 com.example.steblinatetian
这是我的代码:
public class SportAdapter extends Adapter<BaseViewHolder>
private static OnItemClickListener clickListener;
private LayoutInflater inflater;
private List<Sport> sports;
public SportAdapter(Context context, List<Sport> sports)
this.inflater = LayoutInflater.from(context);
this.sports = sports;
static void setOnClickListener(MainActivity mainActivity)
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
return new BaseViewHolder(inflater, parent);
@Override
public void onBindViewHolder(BaseViewHolder holder, final int position)
final Sport sport = sports.get(position);
holder.bind(sport);
SportInfoFragment sportInfoFragment = new SportInfoFragment();
Bundle bundle = new Bundle();
bundle.putString("sport", String.valueOf(sport));
sportInfoFragment.setArguments(bundle);
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent = new Intent(view.getContext(), InfoActivity.class);
if (view.getContext().getResources() != null)
intent.putExtra("sport", sport);
view.getContext().startActivity(intent);
);
@Override
public int getItemCount()
return 20 ;
public OnItemClickListener getClickListener()
return clickListener;
public LayoutInflater getInflater()
return inflater;
public void setInflater(LayoutInflater inflater)
this.inflater = inflater;
void setOnClickListener(OnItemClickListener itemClickListener)
clickListener = itemClickListener;
如何解决这个错误?
【问题讨论】:
您似乎向适配器传递了一个空列表 final Sport sport = sports.get(position);,好像sports是空的 What is a NullPointerException, and how do I fix it?的可能重复 【参考方案1】:在将它放入适配器之前,您必须确保您的列表不为空。为此,请确保在构造函数中添加 @NonNull 注释。
public SportAdapter(Context context, @NonNull List<Sport> sports)
this.inflater = LayoutInflater.from(context);
this.sports = sports;
你在 getItems 方法中也犯了一些错误。改成这个。
@Override
public int getItemCount()
return sports.getSize();
【讨论】:
在我改变了@Override public int getItemCount() return sports.getSize();已发出错误 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on an null object reference at com.example.steblinatetiana.lesson02.SportAdapter.getItemCount(SportAdapter.java: 92) @ТанюшечкаСтеблина 因为您的列表仍然为空。如果您在填充此适配器的位置添加 Activity 或 Fragment 会更容易。【参考方案2】:您的运动列表似乎为空。你可以通过这样做来避免它
public SportAdapter(Context context, List<Sport> sports)
this.inflater = LayoutInflater.from(context);
this.sports = Objects.requireNonNull(sports);
你的应用仍然会崩溃,但至少你会确切地知道原因
【讨论】:
以上是关于适配器抛出 java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPoint
Eclipse无法启动报An internal error occurred during: "reload maven project". java.lang.NullPoint