java - Butterknife 不适用于 Include 标签

标签 java android xml butterknife

我有一个 CoordinatorLayout,其中有我的 Button:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom|end">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:src="@drawable/icon"
        app:fabSize="normal"/>

</android.support.design.widget.CoordinatorLayout>

然后我有包含按钮的 Activity xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    ...

        <include
            android:id="@+id/fabHere"
            layout="@layout/fab_here"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

这里我有一个 Butterknife fragment :

public class MyFragment extends Fragment {
....
@BindView(R.id.fab) FloatingActionButton fab;


    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.my_fragment, container, false);
            ButterKnife.bind(this, view);

            return view;
    }
...
 }

但如果我启动应用程序,则会出现:
java.lang.IllegalStateException:未找到字段“fabHere”的 ID 为 2131624072 的必需 View “fab”。如果此 View 是可选的,请添加“@Nullable”(字段)或“@Optional”(方法)注释。

我尝试添加 '@Nullable' 和 '@Optional' 注释,但它不起作用

最佳答案

只需从 include 标签中删除 id,绑定(bind)就会起作用,即使对于包含的布局中的字段也是如此。

关于java - Butterknife 不适用于 Include 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741828/

相关文章:

java - 根据字符分解字符串

java - 删除带有java文件的文件

android - 从 3.4 更新到 cordova 5.1 后无法获取 appView

Android NDK 中通过 SWIG 的 Java 字符串用奇怪的字符代替空字节

java - 如何避免解析奇怪的字符

java - 当使用 throws 关键字调用 void 时,所有抛出的错误都适用于我调用 void 的地方

java - 在另一个应用程序中从 edittext 读取/写入文本

java - 在不同的开关中使用单选按钮在android中添加变量

c++ - 如何使用 TinyXml 解析特定元素

c# - 使用 Linq 读取 XML 并检查元素是否存在