Butterknife: Include layout cannot bind views

Created on 15 Apr 2016  路  5Comments  路  Source: JakeWharton/butterknife

I am using "include" layouts for my toolbar but i got NPE.

I have a BaseToolbarActivity which is initializing Butterknife.bind(this) and other stuffs.

  • I bind my toolbar @Bind(R.id.toolbar) in BaseToobarActivity

In my SignupActivity extends BaseToolbarActivity, I need to set a toolbar title and other stuffs that is related to toolbar but here is the problem.

I always get NPE in toolbar.

This is my signup_activity.xml

<include
    android:id="@+id/toolbarContainer"
    layout="@layout/toolbar"></include>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbarContainer"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:fillViewport="true"
    android:scrollbars="none">
</ScrollView>

and this is my toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@drawable/pager_bg"
    android:minHeight="?android:attr/actionBarSize"
    app:theme="@style/AppTheme"
    tools:ignore="MissingPrefix">

    <TextView
        android:id="@+id/tvToolbarTitle"
        fontPath="fonts/Roboto-Regular.ttf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:text=""
        android:textAllCaps="true"
        android:textColor="@color/white"
        android:textSize="@dimen/text_font_size_toolbar_5"/>

</android.support.v7.widget.Toolbar>

I used this approach before but now i'm getting some errors.

This is the Log:
D/ButterKnife: Looking up view binder for com.do.de.ui.activity.SignupActivity
D/ButterKnife: HIT: Loaded view binder class.

Most helpful comment

The error is because in include tag id was redefined from toolbar to toolbarContainer so findViewById also will return null because no View with id="@id/toolbar" exists. So butterknife runs as designed.

All 5 comments

Does it work for findViewById?

The error is because in include tag id was redefined from toolbar to toolbarContainer so findViewById also will return null because no View with id="@id/toolbar" exists. So butterknife runs as designed.

Good eyes 馃憖

even i have similar Include tag in my code like above . If i want to declare toolbar using bindView in respective activity..which toolbar id should i need to consider and use it for toolbar initialisation.

In my case i have given toolbar id "hybrid_Act_toolbar" and include tag id also "hybrid_Act_toolbar" Getting below error:
as Caused by: java.lang.IllegalStateException: Required view 'hybrid_Act_toolbar' with ID 2131690576 for field 'hybrid_Act_toolbar' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

error:
03-29 16:38:15.348 21945-21945/com.app.mypoolin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.mypoolin, PID: 21945
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.mypoolin/com.mypoolin.hybrid_upi.activity.InitUPIManageAccountListActivity}: java.lang.IllegalStateException: View 'toolbar' with ID 2131689737 for field 'toolbar' was of the wrong type. See cause for more info.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.IllegalStateException: View 'toolbar' with ID 2131689737 for field 'toolbar' was of the wrong type. See cause for more info.
at butterknife.internal.Utils.castView(Utils.java:113)
at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:105)
at com.mypoolin.hybrid_upi.activity.InitUPIManageAccountListActivity_ViewBinding.(InitUPIManageAccountListActivity_ViewBinding.java:37)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at butterknife.ButterKnife.createBinding(ButterKnife.java:199)
at butterknife.ButterKnife.bind(ButterKnife.java:124)
at com.mypoolin.hybrid_upi.activity.InitUPIManageAccountListActivity.onCreate(InitUPIManageAccountListActivity.java:106)
at android.app.Activity.performCreate(Activity.java:6956)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)聽
at android.app.ActivityThread.-wrap14(ActivityThread.java)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:154)聽
at android.app.ActivityThread.main(ActivityThread.java:6776)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)聽
Caused by: java.lang.ClassCastException: Cannot cast android.support.v7.widget.Toolbar to android.widget.Toolbar
at java.lang.Class.cast(Class.java:2308)
at butterknife.internal.Utils.castView(Utils.java:110)
at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:105)聽
at com.mypoolin.hybrid_upi.activity.InitUPIManageAccountListActivity_ViewBinding.(InitUPIManageAccountListActivity_ViewBinding.java:37)聽
at java.lang.reflect.Constructor.newInstance0(Native Method)聽
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)聽
at butterknife.ButterKnife.createBinding(ButterKnife.java:199)聽
at butterknife.ButterKnife.bind(ButterKnife.java:124)聽
at com.mypoolin.hybrid_upi.activity.InitUPIManageAccountListActivity.onCreate(InitUPIManageAccountListActivity.java:106)聽
at android.app.Activity.performCreate(Activity.java:6956)聽
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)聽
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)聽
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)聽
at android.app.ActivityThread.-wrap14(ActivityThread.java)聽
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)聽
at android.os.Handler.dispatchMessage(Handler.java:102)聽
at android.os.Looper.loop(Looper.java:154)聽
at android.app.ActivityThread.main(ActivityThread.java:6776)聽
at java.lang.reflect.Method.invoke(Native Method)聽
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)聽
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)聽

Hey, did you solve this error? im having trouble with the same thing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pitel picture Pitel  路  3Comments

rajivenator picture rajivenator  路  4Comments

ZakTaccardi picture ZakTaccardi  路  3Comments

alejandro-pnz picture alejandro-pnz  路  3Comments

sdobek picture sdobek  路  4Comments