Butterknife 不适用于 Include 标签 [英] Butterknife does not work with Include tag

查看:41
本文介绍了Butterknife 不适用于 Include 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CoordinatorLayout,其中有我的按钮:

I have a CoordinatorLayout where I have my 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:

And then I have the Activity xml where I include the 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 的片段:

And here I have a fragment with 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"(方法)注释.

But if I start the App this appears:
java.lang.IllegalStateException: Required view 'fab' with ID 2131624072 for field 'fabHere' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

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

I tried to add the '@Nullable' and '@Optional' annotations, but it does not work

推荐答案

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

Simply remove the id from the include tag and the binding will work, even for the fields in the included layout.

这篇关于Butterknife 不适用于 Include 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆