我有一个 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/