1 回答
TA贡献1852条经验 获得超1个赞
像这样改变你Adapter:
public class Adapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
private SetupActivity setupActivity;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
setupActivity = (SetupActivity) context;
}
@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
final View view = layoutInflater.inflate(R.layout.item, container, false);
TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);
buttonDeleteCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setupActivity.deleteSession([...some title...]);
setupActivity.refreshAdapter();
}
});
container.addView(view, 0);
return view;
}
}
为什么会出错?
您将Contextfrom传递SetupActivity给您的Adapter,将该引用保存在字段 ( context) 中,但您错误地创建了SetupActivity.
这就是为什么你得到NullPointerException. 活动是你的Context。它继承自它。因此,只需将其转换为您的构造函数并稍后使用它。
添加回答
举报