JakeWharton / butterknife

Bind Android views and callbacks to fields and methods.
http://jakewharton.github.io/butterknife/
Apache License 2.0
25.55k stars 4.6k forks source link

java.lang.NullPointerException #801

Closed ljf1172361058 closed 7 years ago

ljf1172361058 commented 8 years ago

1. @BindView(R.id.email_sign_in_button)
2. Button emailSignInButton;
3. 
4. @Override
5. protected void onCreate(Bundle savedInstanceState) {
6.         super.onCreate(savedInstanceState);
7.         setContentView(R.layout.activity_login);
8. 
9.         ButterKnife.bind(this);
10. 
11.         // @OnClick()点击事件失效,所以采用该方式测试
12.         // @OnClick() Click event failure, so the test using this way
13.         emailSignInButton.setOnClickListener(new View.OnClickListener() {
14.             @Override
15.             public void onClick(View view) {
16.                 ToastUtils.showShort(LoginActivity.this, "Can help me? thanks.");
17.             }
18.       });
19. }
20.
21.@OnClick(R.id.email_sign_in_button)
22.public void onClick() {
23.        ToastUtils.showLong(this, "output");
24.}

But 13 line error: java.lang.NullPointerException

Module gradle:

dependencies {
    // 编译ButterKnife注解框架(Compile ButterKnife annotation framework)
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

是我的配置出错了么?(Is my configuration error?)

jasoncfpl commented 8 years ago

me too

NikoYuwono commented 8 years ago

@ljf1172361058 Please paste the whole stacktrace

ljf1172361058 commented 8 years ago

@NikoYuwono ok.

jasoncfpl commented 8 years ago

@NikoYuwono hello ,i had this error ,too. my log: 11-11 17:23:01.032 10507-10507/com.diycode.lynn D/ButterKnife: Looking up binding for com.diycode.lynn.LoginActivity 11-11 17:23:01.032 10507-10507/com.diycode.lynn D/ButterKnife: Not found. Trying superclass com.diycode.lynn.TestBaseActivity 11-11 17:23:01.032 10507-10507/com.diycode.lynn D/ButterKnife: Not found. Trying superclass android.app.Activity 11-11 17:23:01.032 10507-10507/com.diycode.lynn D/ButterKnife: MISS: Reached framework class. Abandoning search.

NikoYuwono commented 7 years ago

@cnexcelee Post your code

Khaos116 commented 7 years ago

@ljf1172361058 我测试发现如果使用apply plugin: 'com.neenbedankt.android-apt'会造成空指针

LeifDong commented 7 years ago

@NikoYuwono I have the same problem,my gradle code: // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.2.0' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' } }

allprojects { repositories { jcenter() mavenCentral() }

}

library build.gradle

apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife'

dependencies { compile fileTree(include: '*.jar', dir: 'libs') compile 'com.android.support:design:24.2.1' compile 'com.google.code.gson:gson:2.7' compile 'com.github.chrisbanes.photoview:library:1.2.4' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.squareup.okhttp:okhttp:2.5.0' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' compile 'in.srain.cube:ultra-ptr:1.0.11' compile "com.wefika:flowlayout:0.4.1" }

android { compileSdkVersion 23 buildToolsVersion '23.0.3' useLibrary 'org.apache.http.legacy' sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } } Hope for your help.

Khaos116 commented 7 years ago

@LeifDong
apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife' classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' 这三个是使用本地库的时候需要的 compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 这两个是使用gradle库需要的

分开使用,别一起用

LeifDong commented 7 years ago

@caiyoufei 你好,这是我修改之后的配置,但是依然是java.lang.NullPointerException,我发现编译之后,没有生成XXActivity_ViewBinding.java文件。请问你知道怎么处理吗? apply plugin: 'com.android.library'

dependencies { compile fileTree(include: '*.jar', dir: 'libs') compile 'com.android.support:design:24.2.1' compile 'com.google.code.gson:gson:2.7' compile 'com.github.chrisbanes.photoview:library:1.2.4' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.squareup.okhttp:okhttp:2.5.0' compile 'org.greenrobot:eventbus:3.0.0' compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' compile 'in.srain.cube:ultra-ptr:1.0.11' compile "com.wefika:flowlayout:0.4.1" }

android { compileSdkVersion 23 buildToolsVersion '23.0.3' useLibrary 'org.apache.http.legacy' sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } }

LeifDong commented 7 years ago

dependencies { compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' }

我发现添加以上代码到 app‘s build.gradle 是正确的。 但是添加到library’s build.gradle 就会java.lang.NullPointerException.

taruntak commented 7 years ago

Any solution??

davistsin commented 7 years ago

So, I just use old version to solution this proplem: compile 'com.jakewharton:butterknife:8.2.1' apt 'com.jakewharton:butterknife-compiler:8.2.1'

davistsin commented 7 years ago

In my above everyone whos like me beacuse i am every biger and longer and handsome, look at this !!! replace the 'annotationProcessor ' with 'apt'. sample: dependencies { compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' }

i am your brother.

ljf1172361058 commented 7 years ago

@LeifDong

app's build.gradle:

dependencies {
       compile 'com.jakewharton:butterknife:8.4.0'
       annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

我试了下,真的是这样才有用,目前也只能加载app's build.gradle中用了。 但不知为何只有加在app's build.gradle才有效,而加在library's build.gradle却无效

fvaryu commented 7 years ago

@ljf1172361058` @LeifDong Library projects

To use Butter Knife in a library, add the plugin to your buildscript: ` buildscript { repositories { mavenCentral() } dependencies { classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' } }

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0' } }`

and then apply it in your module: / **这里***/ apply plugin: 'com.android.library' apply plugin: 'com.jakewharton.butterknife'

Now make sure you use R2 instead of R inside all Butter Knife annotations.

class ExampleActivity extends Activity { @BindView(R2.id.user) EditText username; @BindView(R2.id.pass) EditText password; ... }

Library 用法不同 根据你的代码 你那是在library中使用么?

gmyboy commented 7 years ago

use classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8 in your root build.gradle dependencies, add apply plugin: 'android-apt' in your app.gradle, Then use compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' as the app.gradle dependencies. Get it!

ljf1172361058 commented 7 years ago

@fvaryu 是的 我的就是在 library 中使用导致的 NullPointerException

maoruibin commented 7 years ago

If use Butter Knife in a library, you should check this introduce butterknife#library-projects by J.W

maoxiandemeng commented 7 years ago

@ljf1172361058 我也出现这个问题,最后怎么解决的呢

ljf1172361058 commented 7 years ago

@maoxiandemeng 改你主 Module 中的 build.gradle 和项目中的 build.gradle

1.在你主 Module 下 build.gradle 中的 apply plugin: 'com.android.application' 的下面加入

apply plugin: 'android-apt'
...
  1. 在你主 Module 下 build.gradle 中的 dependencies 里加入
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'

    3.在你项目下的 build.gradle 中的 dependencies 里加入 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

maoxiandemeng commented 7 years ago

@ljf1172361058 意思是不用library 形式引入

MrSimpleX commented 7 years ago

@ljf1172361058 那就是在每个Library中都依赖一下 compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' 这两个库吗?我是在项目组件化的时候碰到的这个问题,抽取基础组件的时候在Library里导入butterknife,主工程依赖基础组件,请问你是每个module里都引入了一遍吗

ljf1172361058 commented 7 years ago

@MrSimpleX 我只在 Library 中依赖了 butterknife,主项目依赖该 Library。(PS:我的项目只有主 Moudule 和 Library )

ltigers commented 7 years ago

使用butterknife和rxjava出现view空指针 java.lang.IllegalStateException:Fatal Exception thrown on Scheduler.Worker thread. 2 rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:114) 3 ...... 4 java.lang.NullPointerException:Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference 5 im.pica.pica.ui.TemplateActivity$3.onError(SourceFile:162) 6 rx.observers.SafeSubscriber._onError(SourceFile:153) 7 rx.observers.SafeSubscriber.onError(SourceFile:115) 8 rx.exceptions.Exceptions.throwOrReport(SourceFile:216) 9 rx.observers.SafeSubscriber.onNext(SourceFile:139) 10 rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(SourceFile:224) 11 rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:107) 12 android.os.Handler.handleCallback(Handler.java:756) 13 android.os.Handler.dispatchMessage(Handler.java:95) 14 android.os.Looper.loop(Looper.java:135) 15 android.app.ActivityThread.main(ActivityThread.java:5263) 16 java.lang.reflect.Method.invoke(Native Method) 17 java.lang.reflect.Method.invoke(Method.java:372) 18 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 19 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

ltigers commented 7 years ago

Sometimes normal, something wrong。 I do'nt know why has this issue when activity is destory, i use unbinder.unbind(); Is this causes this error when the network request has not finish READLINK ERROR 4401-21 22:20:57.913 3115 3115 D MtaSDK : [main(1): null:-1] - trackBackground lastForegroundTs:1485037238542 4501-21 22:20:57.913 3115 3256 I MtaSDK : [StatService(190): null:-1] - trackBackground duration:18 4601-21 22:20:57.913 3115 3115 W MtaSDK : [main(1): null:-1] - crash happened, but MTA AutoExceptionCaught is desable. 4701-21 22:20:57.913 3115 3256 D SecShell: READLINK ERROR 4801-21 22:20:57.914 3115 3115 E MtaSDK : 4901-21 22:20:57.914 3115 3115 E MtaSDK : java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread. 5001-21 22:20:57.914 3115 3115 E MtaSDK : at rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:114) 5101-21 22:20:57.914 3115 3115 E MtaSDK : at android.os.Handler.handleCallback(Handler.java:756) 5201-21 22:20:57.914 3115 3115 E MtaSDK : at android.os.Handler.dispatchMessage(Handler.java:95) 5301-21 22:20:57.914 3115 3115 E MtaSDK : at android.os.Looper.loop(Looper.java:135) 5401-21 22:20:57.914 3115 3115 E MtaSDK : at android.app.ActivityThread.main(ActivityThread.java:5263) 5501-21 22:20:57.914 3115 3115 E MtaSDK : at java.lang.reflect.Method.invoke(Native Method) 5601-21 22:20:57.914 3115 3115 E MtaSDK : at java.lang.reflect.Method.invoke(Method.java:372) 5701-21 22:20:57.914 3115 3115 E MtaSDK : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 5801-21 22:20:57.914 3115 3115 E MtaSDK : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 5901-21 22:20:57.914 3115 3115 E MtaSDK : Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError 6001-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber._onError(SourceFile:187) 6101-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onError(SourceFile:115) 6201-21 22:20:57.914 3115 3115 E MtaSDK : at rx.exceptions.Exceptions.throwOrReport(SourceFile:216) 6301-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onNext(SourceFile:139) 6401-21 22:20:57.914 3115 3115 E MtaSDK : at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(SourceFile:224) 6501-21 22:20:57.914 3115 3115 E MtaSDK : at rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:107) 6601-21 22:20:57.914 3115 3115 E MtaSDK : ... 8 more 6701-21 22:20:57.914 3115 3115 E MtaSDK : Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 6801-21 22:20:57.914 3115 3115 E MtaSDK : ... 14 more 6901-21 22:20:57.914 3115 3115 E MtaSDK : Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received => 7001-21 22:20:57.914 3115 3115 E MtaSDK : at android.util.Log.getStackTraceString(Log.java:335) 7101-21 22:20:57.914 3115 3115 E MtaSDK : at android.util.Log.e(Log.java:243) 7201-21 22:20:57.914 3115 3115 E MtaSDK : at com.tencent.legu.stat.common.StatLogger.error(Unknown Source) 7301-21 22:20:57.914 3115 3115 E MtaSDK : at com.tencent.legu.stat.ar.uncaughtException(Unknown Source) 7401-21 22:20:57.914 3115 3115 E MtaSDK : at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693) 7501-21 22:20:57.914 3115 3115 E MtaSDK : at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 7601-21 22:20:57.914 3115 3115 E MtaSDK : at rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:118) 7701-21 22:20:57.914 3115 3115 E MtaSDK : ... 8 more 7801-21 22:20:57.914 3115 3115 E MtaSDK : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.cntysoft.pulltorefresh.PullToRefreshRecyclerView.setPullLoadEnabled(boolean)' on a null object reference 7901-21 22:20:57.914 3115 3115 E MtaSDK : at im.pica.pica.ui.TemplateActivity$3.a(SourceFile:175) 8001-21 22:20:57.914 3115 3115 E MtaSDK : at im.pica.pica.ui.TemplateActivity$3.onNext(SourceFile:146) 8101-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onNext(SourceFile:134) 8201-21 22:20:57.914 3115 3115 E MtaSDK : at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(SourceFile:224) 8301-21 22:20:57.914 3115 3115 E MtaSDK : at rx.android.schedulers.LooperScheduler$ScheduledAction.run(SourceFile:107) 8401-21 22:20:57.914 3115 3115 E MtaSDK : ... 8 more 8501-21 22:20:57.914 3115 3115 E MtaSDK : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference 8601-21 22:20:57.914 3115 3115 E MtaSDK : at im.pica.pica.ui.TemplateActivity$3.onError(SourceFile:162) 8701-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onError(SourceFile:153) 8801-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onError(SourceFile:115) 8901-21 22:20:57.914 3115 3115 E MtaSDK : at rx.exceptions.Exceptions.throwOrReport(SourceFile:216) 9001-21 22:20:57.914 3115 3115 E MtaSDK : at rx.observers.SafeSubscriber.onNext(SourceFile:139) 9101-21 22:20:57.914 3115 3115 E MtaSDK : ... 10 more 9201-21 22:20:57.914 3115 3115 D MtaSDK : [main(1): null:-1] - Call the original uncaught exception handler. 9301-21 22:20:57.914 3115 3115 E CrashReport: Java Crash Happen cause by main(1) 9401-21 22:20:57.914 3115 3260 D MID : [StatStore(194): null:-1] - readNewVersionMidEntity:{"imei":"354926050849775","imsi":"310260050849775","mac":"52:54:00:55:93:EF","mid":"fd39484b0c22d036d302772860077b44912bf803","ts":1485037213279} 9501-21 22:20:57.915 3115 3260 I MtaSDK : [StatStore(194): null:-1] - insert 1 event, content:{"ky":"Alg1105696304","et":4,"ui":"354926050849775","mc":"52:54:00:55:93:EF","ut":0,"ia":1,"av":"1.0.1","ch":"应用宝","cch":"legu","mid":"fd39484b0c22d036d302772860077b44912bf803","idx":8,"si":135590512,"ts":1485037257,"dts":0,"os":1,"midver":"3.6","new_mid":"fd39484b0c22d036d302772860077b44912bf803","pcn":"im.pica.pica","ov":"22","md":"GI-I9500TMMARS","jb":1,"mf":"TrendMicro","ifg":1,"bc":2,"ft":1,"du":18} 9601-21 22:20:57.915 3115 3115 I System.out: TMMARS: command executed from JavaRuntime [ logcat -d -v threadtime] 9701-21 22:20:57.916 3115 3260 D MID : [StatStore(194): null:-1] - readNewVersionMidEntity:{"imei":"354926050849775","imsi":"310260050849775","mac":"52:54:00:55:93:EF","mid":"fd39484b0c22d036d302772860077b44912bf803","ts":1485037213279} 9801-21 22:20:57.917 3669 3669 W MARSLOG : |FORK|||getpid=3669 getuid=10038 9901-21 22:20:57.917 3115 3115 W MARSLOG : |FORK|||getpid=3115 getuid=10038 10001-21 22:20:57.917 3115 3260 D MtaSDK : [StatStore(194): null:-1] - directStoreEvent insert event to db, event:{"ky":"Alg1105696304","et":4,"ui":"354926050849775","mc":"52:54:00:55:93:EF","ut":0,"ia":1,"av":"1.0.1","ch":"应用宝","cch":"legu","mid":"fd39484b0c22d036d302772860077b44912bf803","idx":8,"si":135590512,"ts":1485037257,"dts":0,"os":1,"midver":"3.6","new_mid":"fd39484b0c22d036d302772860077b44912bf803","pcn":"im.pica.pica","ov":"22","md":"GI-I9500_TMMARS","jb":1,"mf":"TrendMicro","ifg":1,"bc":2,"ft":1,"du":18} 10101-21 22:20:57.918 3115 3115 D SecShell: filename:/dev/ashmem 0 20480

ltigers commented 7 years ago

空指针部分机型会出现:华为 GRA-CL00

rch0903 commented 7 years ago

I got this problem too,and i use it in app's build like this: compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'; My solution is delete 'apt' in project's build and app's build,I think the 'apt' is conflicted with 'annotationProcessor',only one can be retained.

dongjielitianjiao commented 7 years ago

@ljf1172361058 我用的rxbinding 一直报空指针,解决不了呢

JakeWharton commented 7 years ago

It's not clear what the problem is here, but it seems like code isn't being generated or packaged into the APK. Ensure proper build configuration, that the generated classes are in build/, and that they end up in the final APK with APK inspector.

ChasonChen commented 7 years ago

在多module开发中,如果当前项目butterknife依赖来自于其他module,则会导致当前项目使用时空指针。确保项目当前目录下的build.gradle文件中加入butterknife依赖,才能保证当前项目中使用时不会空指针。

ljf1172361058 commented 7 years ago

是的,所以我现在把 butterknife 依赖都放在我的主 module 中来避免空指针

MoMuBai commented 6 years ago

butterknife8.0以下的时候,正常使用 compile 'com.jakewharton:butterknife:7.0.1'就可以,但是在butterknife8.0以上,使用implementation 'com.jakewharton:butterknife:8.5.1'(AndroidStudio3.0)一直报NullPointerException,后来又继续添加了annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'才避免了NullPointerException

GreenPepperForPotato commented 6 years ago

第一阶段:下面配置可以正常工作,生成对应的xxxActivity_ViewBinding dependencies{ implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT' annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT' }

第二阶段:新项目添加了kotlin环境,在app.gradle中添加了下面的代码,就不能编译自动生成xxxActivity_ViewBinding了,到了指定界面就报NullPointerException

apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' // 经过排查是这段代码产生了影响

解决办法有两种: 1、apply plugin: 'kotlin-kapt' 这行代码注释,(不推荐) 2、把annotationProcessor这一块改成kapt。就可以了。kapt 也能够处理Java 文件,所以不需要再保留 annotationProcessor 的依赖 dependencies{ implementation 'com.jakewharton:butterknife:9.0.0-SNAPSHOT' kapt 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT' }