升级 ButterKnife 时将 findByID 转换为 @BindView 时出错

Error convert findByID to @BindView when upgrading ButterKnife

我有一个别人的项目,但是当我打开它的时候出现了一些错误。 Butterknife报错,显示:“error: can't find findById (View, int) notation”,我知道已经被@BindView替代了,我把findById改成了@BindView但是只有findById 2个参数,而@BindView只有1个,怎么转换?

这是新代码:

@BindView(R.id.fab_subitem_image) ImageView image;
private void addActionItem(@NonNull LayoutInflater inflater, int index,
    @NonNull final FABAction item) {
    // Inflate & Configure item
    final View subItem = inflater.inflate(R.layout.fab_subitem, mItemContainer, false);

    image.setBackgroundColor(item.mBgColor);

这是旧代码:

private void addActionItem(@NonNull LayoutInflater inflater, int index,
    @NonNull final FABAction item) {
    // Inflate & Configure item
    final View subItem = inflater.inflate(R.layout.fab_subitem, mItemContainer, false);
    ImageView image = ButterKnife.findById(subItem, R.id.fab_subitem_image); <--This is old code

    image.setBackgroundColor(item.mBgColor);

您必须告诉 ButterKnife 为包含 @BindView 注释的对象生成绑定。你应该在 inflation:

之后调用 ButterKnife.bind(this, subItem)
@BindView(R.id.fab_subitem_image) ImageView image;

private Unbinder unbinder;

private void addActionItem(@NonNull LayoutInflater inflater, int index, @NonNull final FABAction item) {
    // Inflate & Configure item
    final View subItem = inflater.inflate(R.layout.fab_subitem, mItemContainer, false);
    // Generate bindings
    unbinder = ButterKnife.bind(this, subItem);

    image.setBackgroundColor(item.mBgColor);
    ...
}

...

@Override
public void onDestroyView(View view) {
    // Unbind when bindings are no longer needed eg. in onDestroyView of a Fragment
    unbinder.unbind()
}

有关详细信息,请参阅 the documentation