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

报错

 Caused by: java.lang.RuntimeException: Unable to bind views for cc.haoduoyu.umaru.ui.activities.ChatActivity at butterknife.ButterKnife.bindButterKnife.java:322 at butterknife.ButterKnife.bindButterKnife.java:237 at cc.haoduoyu.umaru.base.ToolbarActivity.onCreateToolbarActivity.java:42 at cc.haoduoyu.umaru.ui.activities.ChatActivity.onCreateChatActivity.java:81 at android.app.Activity.performCreateActivity.java:6285 at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1108 at android.app.ActivityThread.performLaunchActivityActivityThread.java:2369 at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2476  at android.app.ActivityThread.-wrap11ActivityThread.java  at android.app.ActivityThread$H.handleMessageActivityThread.java:1344  at android.os.Handler.dispatchMessageHandler.java:102  at android.os.Looper.loopLooper.java:148  at android.app.ActivityThread.mainActivityThread.java:5417  at java.lang.reflect.Method.invokeNative Method  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:726  at com.android.internal.os.ZygoteInit.mainZygoteInit.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.findRequiredViewButterKnife.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 { } @BindR.id.app_bar_layout protected AppBarLayout mAppBar; @BindR.id.toolbar protected Toolbar mToolbar; @Override protected void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; setContentViewprovideContentViewId; ButterKnife.bindthis; }
}

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

这个不是找不到id资源

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

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注