ButterKnife绑定include布局失败

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24671941/article/details/82458065

问题:

 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);
    }
}

原因:

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

其实从原理出发就会很容易明白,我们要绑定的是include中的控件,而不是include,所以自然要为其中的控件定义标识id,要不然无法识别要绑定的对象。并且ButterKnife的本质就是使用了findViewById,如果手动通过findViewById方法也是先获取到include对象,再从中findViewById要绑定的控件。

解决办法:

只写layout,不要加+id

<include
    layout="@layout/view_toolbar">
</include>

 

猜你喜欢

转载自blog.csdn.net/qq_24671941/article/details/82458065