1. 程式人生 > >ButterKnife繫結include佈局失敗

ButterKnife繫結include佈局失敗

問題:

 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>