为什么给layout加上id,ButterKnife就找不到资源了?

报错

 Caused by: java.lang.RuntimeException: Unable to bind views for cc.haoduoyu.umaru.ui.activities.ChatActivity
                                                                       at butterknife.ButterKnife.bind(ButterKnife.java:322)
                                                                       at butterknife.ButterKnife.bind(ButterKnife.java:237)
                                                                       at cc.haoduoyu.umaru.base.ToolbarActivity.onCreate(ToolbarActivity.java:42)
                                                                       at cc.haoduoyu.umaru.ui.activities.ChatActivity.onCreate(ChatActivity.java:81)
                                                                       at android.app.Activity.performCreate(Activity.java:6285)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:148) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                    Caused by: java.lang.IllegalStateException: Required view 'app_bar_layout' with ID 2131691083 for field 'mAppBar' was not found. If this view is optional add '@Nullable' annotation.
                                                                       at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)

activity.xml

 <include
            android:id="@+id/toolbarLayout"//加上这行就报错
            layout="@layout/view_toolbar"></include>

view_toolbar.xml

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

activity

public abstract class Activity extends BaseActivity {

    abstract protected int provideContentViewId();

    public void onToolbarClick() {
    }

    @Bind(R.id.app_bar_layout)
    protected AppBarLayout mAppBar;
    @Bind(R.id.toolbar)
    protected Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideContentViewId());
        ButterKnife.bind(this);
    }
}
阅读 15k
4 个回答

id为什么不加到标签里面的布局去呢

这个不是找不到id资源

不记得原因了,的确是不能在include布局里面加id,除了butterknife外还有其他情况也会出错,比如include里面的内容和外部内容的id重复了也会出错。

include标签会把include标签的id的属性和visibility属性覆盖到include标签的layout的根节点上, 所以你的toolbarLayout这个id覆盖掉了app_bar_layout

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏