我有一个CoordinatorLayout,我有我的按钮:
<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,其中包含Button:
<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'注释,但它不起作用
答案 0 :(得分:11)
只需从include标记中删除id即可,即使对于包含的布局中的字段,绑定仍然有效。
答案 1 :(得分:2)
MainActivity,
public MainActivity extends AppCompatActivity {
// 1. First, we declare the layout that was included as a View objects.
@BindView( R.id.layout_1 ) View layout_1;
@BindView( R.id.layout_2 ) View layout_2;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
// 2. In here, we bind the included layouts
ButterKnife.bind( this );
// 4. Then, we create objects of the type of the IncludedLayout.
// In this example the layout reuse the same layout twice, so, there are two
// IncludedLayouts.
IncludedLayout includedLayout_1 = new IncludedLayout();
IncludedLayout includedLayout_2 = new IncludedLayout();
// 5. We bind the elements of the included layouts.
ButterKnife.bind( includedLayout_1, layout_1 );
ButterKnife.bind( includedLayout_2, layout_2 );
// 6. And, finally, we use them.
includedLayout_1.displayed_text.setText( "Hello" );
includedLayout_2.displayed_text.setText( "Hey!" );
}
// 3. We create a static class that will be an container of the elements
// of the included layout. In here we declare the components that
// hold this. In this example, there is only one TextView.
static class IncludedLayout {
@BindView( R.id.displayed_text ) TextView displayed_text;
}
}
MainActivity的XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include android:id="@+id/layout_1" layout="@layout/included_layout" />
<include android:id="@+id/layout_2" layout="@layout/included_layout" />
</LinearLayout>
包含布局的XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/displayed_text"/>
</LinearLayout>
就是这样!
信用:Reference
答案 2 :(得分:0)
作者@JakeWharton说 -
Put an ID on the <include> and bind it to an arbitrary View field for each one.
Then create an object which binds the inner views
static class Something {
@Bind(R.id.something) TextView somethingView;
}
You can then create multiple instances of that object
and call ButterKnife.bind(something1, include1) and
ButterKnife.bind(something2, include2) and so on.
Consider also just making a custom view instead of using <include> which has
proper APIs to get its children (or at least binds them to fields itself).
答案 3 :(得分:0)
我建议您使用Google提供的Data Binding Library代替Butterknife
答案 4 :(得分:0)
由于这个问题已经很久了。
如今(2019年10月),我可以在include标签中连接我的ID。
在深入研究之前,尝试做类似原始问题的事情。
@BindView(R.id.your_button_id_inside_include_tag) Button button;