Butter Knife在adapter

今天使用Butter knife在adapter中使用,一直不成功,总是报错!仔细想了一下,也是如果官网介绍的那样使用的:

public class MyAdapter extends BaseAdapter {
  @Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view != null) {
      holder = (ViewHolder) view.getTag();
    } else {
      view = inflater.inflate(R.layout.whatever, parent, false);
      holder = new ViewHolder(view);
      view.setTag(holder);
    }

    holder.name.setText("John Doe");
    // etc...

    return view;
  }

  static class ViewHolder {
    @InjectView(R.id.title) TextView name;
    @InjectView(R.id.job_title) TextView jobTitle;

    public ViewHolder(View view) {
      ButterKnife.inject(this, view);
    }
  }
}
结果总是报错,仔细查看了下原因,原来其中的一个TextView并不在这个adapter所在是XML视图中,哎~!低级的错误,让我找了快2个小时!


你可能感兴趣的:(Butter Knife在adapter)