【问题标题】:Error convert findByID to @BindView when upgrading ButterKnife升级 ButterKnife 时将 findByID 转换为 @BindView 时出错
【发布时间】:2019-10-14 10:52:22
【问题描述】:

我有一个其他人的项目,但是当我打开它时出现了一些错误。 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.bind(this); or ButterKnife.bind(this, subItem);了吗?

标签: android android-studio mobile replace butterknife


【解决方案1】:

您必须告诉 ButterKnife 为包含 @BindView 注释的对象生成绑定。你应该在通货膨胀之后打电话给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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多