我的适配器:
public class Adaptor extends RecyclerView.Adapter<Adaptor.Holder>{
private ArrayList<Winkel> winkels;
private LayoutInflater inflater;
private ImageView icon;
private ItemCLickCallback itemCLickCallback;
public interface ItemCLickCallback {
void onItemClick(int p);
void onSecItemClick(int p);
}
public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){
this.itemCLickCallback = itemCLickCallback;
}
public Adaptor (ArrayList<Winkel> winkels,Context c){
this.inflater = LayoutInflater.from(c);
this.winkels = winkels;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_item, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
Winkel winkel = winkels.get(position);
holder.title.setText(winkel.getNaam());
if (winkel.isFavourtite()){
holder.icon.setImageResource(R.drawable.ic_star_black_18dp);
}
else{
holder.icon.setImageResource(R.drawable.ic_star_border_black_18dp);
}
}
@Override
public int getItemCount() {
return winkels.size();
}
class Holder extends RecyclerView.ViewHolder implements View.OnClickListener{
private TextView title;
private View container;
private ImageView icon;
public Holder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.lbl_item_text);
container = itemView.findViewById(R.id.cont_item_root);
icon = (ImageView) itemView.findViewById(R.id.im_item_icon_secondary);
icon.setOnClickListener(this);
container.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.cont_item_root){
Log.d("lol",Integer.toString(getAdapterPosition()));
itemCLickCallback.onItemClick(getAdapterPosition());
}
else{
itemCLickCallback.onSecItemClick(getAdapterPosition());
}
}
}
}
我的概述片段:
@Override
public void onItemClick(int p) {
Log.d("lol",(Integer.toString(p)));
Winkel winkel = (Winkel) mijnwinkels.get(p);
Bundle detailsBundle = new Bundle();
detailsBundle.putString(DETAILS_NAAM,winkel.getNaam());
detailsBundle.putString(DETAILS_ADRES,winkel.getAdres());
detailsBundle.putString(DETAILS_DEELGEMEENTE,winkel.getDeelGemeente());
detailsBundle.putString(DETAILS_GEMEENTE,winkel.getGemeente());
detailsBundle.putString(DETAILS_POSTCODE,winkel.getPostcode());
Details detail= new Details();
detail.setArguments(detailsBundle);
this.getFragmentManager().beginTransaction()
.replace(R.id.details,detail,null)
.addToBackStack(null)
.commit();
}
错误讯息:
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.hoofdgebruiker.winkelskortrijk.Utill.Adaptor$ItemCLickCallback.onItemClick(int)' on a null object reference.
我的代码正在努力,我得到了recyclerview,项目在其中,但我不能点击其中任何一个而不会出现此错误,我不明白为什么。我做错了它是一个空对象引用?我的日志都没有显示出来。
编辑:我的方法onItemCLickCallback在这里使用:
@Override
public void onSuccess(ArrayList<Winkel> winkels) {
mijnwinkels = winkels;
Adaptor adaptor = new Adaptor(mijnwinkels, this.getActivity());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mRecyclerView.addItemDecoration(new VerticalSpace(30));
mRecyclerView.setAdapter(adaptor);
adaptor.setItemCLickCallback(this);
}
答案 0 :(得分:1)
您的方法参数中存在拼写错误
public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){
this.itemCLickCallback = itemCLickCallback;
}
您期待itemCLickCallback1并指定itemCLickCallback(注意额外&#39; 1&#39;作为后缀的区别)。