ButterKnife 8.0.1 not working

33,354

Solution 1

Per the readme, you need to include the butterknife-compiler in order for the generated code to be produced automatically:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

Without this there is no generated code to be loaded and thus none of the fields get set.

You can verify ButterKnife is working by calling ButterKnife.setDebug(true) and looking in Logcat.

Solution 2

I used this library in Fragment and has NPE. My code was:

ButterKnife.bind(view);

But it was wrong. Library need to know two objects:
1) Target - with annotations @BindView
2) Source - with views

It will be right to write:

ButterKnife.bind(this, view);

When this - your fragment, and view - view of this fragment.

Solution 3

for me the problem was that I was using

annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'

instead of

 apt 'com.jakewharton:butterknife-compiler:8.7.0

Solution 4

App Level(build.gradle)

apply plugin: 'android-apt'
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}


Project Level(build.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Solution 5

Config Butterknife on build.gradle file like this,

compile("com.jakewharton:butterknife:8.5.1")
annotationProcessor "com.jakewharton:butterknife-compiler:8.5.1"

It works for me.

Share:
33,354
Sebastian Corradi
Author by

Sebastian Corradi

Linkedin profile: https://www.linkedin.com/in/sebastoian Bacherlor's Degree in Systems Analysis. Passionate in everything I do, I like to learn new things, met new people and cultures. By Day: Android Developer By Afternoon: Gym fan. By Night: Percussionist That's it

Updated on March 24, 2021

Comments

  • Sebastian Corradi
    Sebastian Corradi about 3 years

    I am using butterknife 8.0.1, but a nullpointerexception is appearing.

    This line is on my build.grade file: compile 'com.jakewharton:butterknife:8.0.1'

    this is my Main Class: (I wrote the includes properly)

    import butterknife.BindView;
    import butterknife.ButterKnife;
    
    public class MainActivity extends BaseActivity {
    
        @BindView(R.id.MainScreenTextView) TextView mainText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ButterKnife.bind(this);
    
            **mainText.setText("Butter knife is working fine");**
        }
    

    and this is MainActivity.xml:

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    
    </android.support.design.widget.AppBarLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/MainScreenTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is the Main Screen"
            android:textColor="#000000"
            android:background="#666666"
            android:padding="5dp"
            android:textSize="20dp"/>
    </LinearLayout>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />