无法在ButterKnife.bind上绑定.. $ RecyclerViewHolders的视图(this,itemView);在RecyclerViewHolders中

时间:2016-04-25 11:18:11

标签: android android-studio android-recyclerview recycler-adapter butterknife

我收到错误日志:

  

java.lang.RuntimeException:无法绑定.. $ RecyclerViewHolders的视图   在butterknife.ButterKnife.bind(ButterKnife.java:322)                                                                               在butterknife.ButterKnife.bind(ButterKnife.java:279)   ...    在android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:5482)                                                                               在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4707)                                                                               在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4617)                                                                               在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:1994)                                                                               在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

    private Context mContext;


    @Bind(R.id.tvRowServiceCenterName)
    CustomTextView tvRowServiceCenterName;
    @Bind(R.id.tvRowServiceCenterKmsValue)
    CustomTextView tvRowServiceCenterKmsValue;

    @Bind(R.id.ivRowServiceCenterImage)
    CircleImageView ivRowServiceCenterImage;
    @Bind(R.id.ivRowServiceCenterStatus)
    CircleImageView ivRowServiceCenterStatus;

    public RecyclerViewHolders(Context context, View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView); // Getting error here at runtime
        this.mContext = context;
        //itemView.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

    }
}

我也指Butterknife is unable to bind inside my Adapter Class

另见ButterKnife.bind(this, itemView);相关问题。

但它无法帮助我。我错过了什么,或做错了什么?

1 个答案:

答案 0 :(得分:3)

您可以尝试以下代码:

public static class ViewHolder extends RecyclerView.ViewHolder{
@Bind(R.id.tvRowServiceCenterName)
CustomTextView tvRowServiceCenterName;
@Bind(R.id.tvRowServiceCenterKmsValue)
CustomTextView tvRowServiceCenterKmsValue;
@Bind(R.id.ivRowServiceCenterImage)
CircleImageView ivRowServiceCenterImage;
@Bind(R.id.ivRowServiceCenterStatus)
CircleImageView ivRowServiceCenterStatus;

private ViewHolder(View view, int viewType, Context context){
        super(view);
        ButterKnife.bind(this, view);
    }
}