2016-07-29 47 views
-1

我的適配器:Recyclerview上點擊空對象引用

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. 

我的代碼是工作到這種地步我讓我回收看,這些項目是在它,但我不能點擊任何一個沒有得到這個錯誤,我不明白爲什麼。我做錯了,它是一個空對象引用?我的日誌都沒有出現。

編輯:我的方法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

@R.Zagórski我有一個參考,我添加了代碼。我只是沒有看到我在做錯什麼,我在技術上知道如何解決nullpointerexception,我只是不知道如何解決這個代碼? – user3117628

回答

1

。在你的方法參數

public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback1){ 
    this.itemCLickCallback = itemCLickCallback; 
} 

你期待itemCLickCallback1和分配itemCLickCallback(注意額外的「差的錯字1 '作爲後綴)。

+0

:)謝謝你,非常愚蠢的錯誤,我正要重寫整個事情。謝謝 – user3117628

相關問題