当我尝试为我的recyclerview放置clicklistner时出现错误。在新的片段中打开有关该特定项目的信息。
这是我的代码:
模型类:
public class Banner extends UserId implements Serializable {
String mSliderImage;
public Banner() {
}
public Banner(String mSliderImage) {
this.mSliderImage = mSliderImage;
}
public String getmSliderImage() {
return mSliderImage;
}
public void setmSliderImage(String mSliderImage) {
this.mSliderImage = mSliderImage;
}
}
适配器类:
public class HeaderSliderAdapter extends RecyclerView.Adapter<HeaderSliderAdapter.ViewHolder> {
public List<Banner> banners;
public Context context;
private HeaderSliderAdapter.OnItemClicked onClick;
public HeaderSliderAdapter(Context context, List<Banner> banners) {
this.banners = banners;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_header, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int pos) {
final int position = pos % banners.size();
if (TextUtils.isEmpty(banners.get(pos).getmSliderImage())) {
holder.mSliderImageView.setImageResource(R.drawable.app_logo);
} else {
Picasso.with(context)
.load(banners.get(pos).getmSliderImage())
.fit()
.into(holder.mSliderImageView, new Callback() {
@Override
public void onSuccess() {
holder.mBackgroundImage.setVisibility(View.GONE);
}
@Override
public void onError() {
holder.mBackgroundImage.setVisibility(View.VISIBLE);
}
});
}
holder.mSliderImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(pos, banners.get(pos));
}
});
}
@Override
public int getItemCount() {
return banners.size();
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
public interface OnItemClicked {
void onItemClick(int position, Banner passData);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView mSliderImageView,mBackgroundImage;
public View click;
View mView;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mSliderImageView = mView.findViewById(R.id.mSliderImage);
mBackgroundImage = mView.findViewById(R.id.backgroundImage);
}
}
}
Java Class:
@Override
public void onItemClick(int pos, Banner passData) {
android.support.v4.app.FragmentManager fm = getFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container_dashboard, HeaderBannerInfo.newInstance(banners.get(pos).getmSliderImage()));
ft.addToBackStack(null); // this line responsible for adding fragment in backstack.
ft.commit();
}
我的logcat中出现错误:
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.info.lootbox.Adapter.HeaderSliderAdapter$OnItemClicked.onItemClick(int, com.example.info.lootbox.Model.Banner)' on a null object reference
at com.example.info.lootbox.Adapter.HeaderSliderAdapter$2.onClick(HeaderSliderAdapter.java:66)
这是我的适配器的66号线
holder.mSliderImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClick.onItemClick(pos, banners.get(pos));
}
});
}