Butter Knife return null pointer

不问归期 提交于 2019-12-03 23:21:28

I fixed it.

There is a problem with my build.gradle

I forgot to add

apt 'com.jakewharton:butterknife-compiler:8.0.1'

to the build.gradle

Thank everyone

UPDATE

If you are using neenbedankt.android-apt plugin first remove it.

Then remove apt 'com.jakewharton:butterknife-compiler:8.0.1'

And then add annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' to the dependencies.

UPDATE 2

If you are using kotlin replace :

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

with:

kapt 'com.jakewharton:butterknife-compiler:8.8.1'

And don't forget to add

apply plugin: 'kotlin-kapt'

after:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
Jerry Frost

In your onCreate method, make sure you have the line:

ButterKnife.bind(this);

Without that line, the binds you set up aren't performed, and the views remain null.

Use the following if your are using the new Butter Knife version:

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

If you are using Kotlin, replace annotationProcessor with kapt.

UPDATE:

If you are using Gradle plugin 3.0 or above in your project, change compile to implementation. like below:

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Yep, Butterknife by Jake Wharton has been updated to 8.0.1

Please refer at his git account for steps Butterknife Git

On final note : Make sure the line apply plugin ... is placed somewhere at the top of the file.

I started getting NPE errors when, on an existing project, I added support for DataBinding and Kotlin.

I had:

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

..and replaced with:

kapt "com.jakewharton:butterknife-compiler:x.x.x"

I got the same exception. In my case I forgot to add jcenter() repository in my app-module build.gradle file.

buildscript {
    repositories {
        mavenCentral()
        //this was missed
        jcenter()
    }
    dependencies {
        ...
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!