【发布时间】:2016-11-22 12:25:56
【问题描述】:
我有一个 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 的片段:
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 的必需视图“fab”。如果此视图是可选的,请添加“@Nullable”(字段)或“@Optional”(方法)注释。
我尝试添加“@Nullable”和“@Optional”注释,但不起作用
【问题讨论】:
-
如果你删除
android:id="@+id/fabHere"会发生什么? -
什么也没发生
-
什么也没发生?它是否崩溃,它没有显示任何内容?使用
Butterknife.setDebug(true) -
同样的错误:java.lang.IllegalStateException:未找到字段“fab”的 ID 为 2131624072 的必需视图“fab”。如果此视图是可选的,请添加“@Nullable”(字段)或“@Optional”(方法)注释。
-
@SarahPöhler 检查此链接github.com/JakeWharton/butterknife/issues/396
标签: java android xml butterknife