Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quoting Butter Knife 8.8.1 in the library, using BindView in the app module, but the view appears NullPointerException #1127

Open
qinxianyuzou opened this issue Oct 30, 2017 · 34 comments

Comments

@qinxianyuzou
Copy link

I used a butterknife 8.8.1 in AndroidStudio3.0。
My project gradle:

buildscript {
    repositories {
        jcenter()
        ......
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
    }
}
allprojects {
    repositories {
        jcenter()
        ......
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        google()
    }
}

In my app module gradle:

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

In my Library module gradle:

apply plugin: 'com.android.library'
dependencies {
    ......
    compile "com.jakewharton:butterknife:${butterknifeSdkVersion}"
    annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeSdkVersion}"
}

In my app module activity binding View, @ BindView (R.id.xx) and @BindView (R2.id.xx) have tried, but still NullPointerException.Why is that?


My English is relatively poor, the above text from the Google translation, grammar unreasonable please understand

@JakeWharton
Copy link
Owner

Turn on debug logging on the ButterKnife object. What logs are printed? Can you see the generated code in your build/generated/source/ folder? In the APK?

@qinxianyuzou
Copy link
Author

qinxianyuzou commented Oct 30, 2017

Thanks @JakeWharton reply.
I open the butter knife debug, run the app, the logcat shows the following log:

......
D/ButterKnife: Looking up binding for com......activity.MainActivity
D/ButterKnife: Not found. Trying superclass com......BaseActivity
D/ButterKnife: Not found. Trying superclass android.support.v7.app.AppCompatActivity
D/ButterKnife: MISS: Reached framework class. Abandoning search.
......

My app module in build/generated/source/ no ViewBinder file

Turn on debug logging on the ButterKnife object. What logs are printed? Can you see the generated code in your build/generated/source/ folder? In the APK?


A supplemental information, my project has three layers:

  • commonLibrary -> commonService(library) -> app module
  • butterknife in commonLibrary,commonService dependent on commonLibrary.
  • the commonService is some basic class,BaseActivity is here.
  • app module dependent on commonService,MainActivity extends BaseActivity.

I do not know if it is affected by this structure.
In commonService try to add the "apply plugin: 'com.android.library'" and "apply plugin: 'com.jakewharton.butterknife'", but the same report NullPointerException

@imaiya
Copy link

imaiya commented Oct 30, 2017

I encountered the same problem,user gradle:3.0.0 and butterknife 8.8.1;

  • ButterKnife.bind(this) in library,
  • @BindView(R.id.xx) in app,
    exception :NullPointerException

@qinxianyuzou 兄弟你这个问题解决了吗,我升级到3.0遇到这个问题,很烦

@qinxianyuzou
Copy link
Author

@imaiya 并没有,其实有个简单的解决办法,直接把butterknife挂在app module里面,而不在library里挂,这样就不会出现问题,不过这么搞不符合组件开发而已。不仅仅是在studio3.0,在2.3的时候用butterknife挂在library里也是会这样的,所以我不懂是不是配置没有配置正确还是butterknife有bug

I encountered the same problem,user gradle:3.0.0 and butterknife 8.8.1;
ButterKnife.bind(this) in library,
@bindview(R.id.xx) in app,
exception :NullPointerException
@qinxianyuzou 兄弟你这个问题解决了吗,我升级到3.0遇到这个问题,很烦

@imaiya
Copy link

imaiya commented Oct 30, 2017

@qinxianyuzou 对啊,我这边是有基础library,在library的基类中bind(this),在app具体页面中使用bindview,之前使用7.x是没有问题的,但是因为升级3.0需要使用8.x,升级之后就报空了;如果单独拎出来会破坏基础库的机构,不是很想这样做,如果找到好的解决方案callback一下哈

@qinxianyuzou
Copy link
Author

qinxianyuzou commented Oct 30, 2017

我的项目更加复杂,项目架构是主管架构的,一个基础library库,然后一个基类library库,最后才是app的一层依赖一层,我不知道是不是这个原因导致了butterknife失效了,如果是,那就麻烦了

@qinxianyuzou 对啊,我这边是有基础library,在library的基类中bind(this),在app具体页面中使用bindview,之前使用7.x是没有问题的,但是因为升级3.0需要使用8.x,升级之后就报空了;如果单独拎出来会破坏基础库的机构,不是很想这样做,如果找到好的解决方案callback一下哈

@ranjitzade
Copy link

ranjitzade commented Oct 30, 2017

@JakeWharton I am getting the same error in android studio 3.0.0 and butterknife 8.8.1

Not found. Trying superclass xx.xx.xxx..BottomTabsActivity
Not found. Trying superclass xx.xx.xxx..ParentActivity
Not found. Trying superclass android.support.v7.app.AppCompatActivity
MISS: Reached framework class. Abandoning search.

@wvisible
Copy link

@qinxianyuzou @imaiya Try adding 'annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'' to the module

@qinxianyuzou
Copy link
Author

qinxianyuzou commented Oct 31, 2017

@wvisible Is it added to the app module instead of added to the library?

@wvisible
Copy link

@qinxianyuzou yes,you can try it.

@imaiya
Copy link

imaiya commented Oct 31, 2017

@wvisible @qinxianyuzou Thank you for the suggestion that I used this method to solve the problem

@qinxianyuzou
Copy link
Author

@wvisible You're right, this method can solve the problem, but doing so is tantamount to direct the butter knife on the app module Nothing to do with the library, which is not in line with the idea I put the butter knife into the Library.I hope that the library is perfect and independent, not every time need to add in the app module

@wangyuetingtao
Copy link

在defaultConfig中加入
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
就可以继续用7.0.1啦,当前最新版本的问题还请 @JakeWharton 大神解决下

@wimwu
Copy link

wimwu commented Nov 1, 2017

you can add

dependencies {
    ......
    compile "com.jakewharton:butterknife:${butterknifeSdkVersion}"
    annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeSdkVersion}"
}

in app module build.gradle too.

@qinxianyuzou
Copy link
Author

@wimvogt Yes, I know this, but it does not solve the problem of using a butterknife in a library.

you can add

dependencies {
......
compile "com.jakewharton:butterknife:${butterknifeSdkVersion}"
annotationProcessor "com.jakewharton:butterknife-compiler:${butterknifeSdkVersion}"
}
in app module build.gradle too.

@Drakot
Copy link

Drakot commented Nov 7, 2017

I deleted apply plugin: 'com.jakewharton.butterknife' and It worked

@nirmal912
Copy link

did anyone find a solution? I am facing the same problem in gradle:3.0.0

@fauzanmakky
Copy link

use gradle:3.0.0 and butterknife 8.8.1;
when i open my apps, my apps running well, but when i accses view to often in some frgament (replace fragment) in short time, the view being null.
@JakeWharton

@BansookNam
Copy link

BansookNam commented Nov 8, 2017

I had no problem after change to Android Studio 3.0,
gradle 3.0.0, with butterknife 8.8.1
I'm using butterknife in app module. (not library)

However after change part of code to -> "kotlin".
the problem happened in a same way.

Build has no problem but after run the app.
all views with @BindView is null.
of course I called Butterknife.bind(). however it dosen't work.
(Ps. the problem happened with using @BindView in a .java file)

need a help.

@kassim
Copy link

kassim commented Nov 8, 2017

@BansookNam are you using kapt instead of annotationProcessor for your Butterknife dependency statement in your build.gradle?

I had the same issue when introducing Kotlin - you need to use kapt for anything that you want to touch Kotlin code

@BansookNam
Copy link

@kassim Thank you kassim
I just used "kapt" a minute ago and solved the problem.
have a good day~!

@shenwenbin123
Copy link

@qinxianyuzou I have the same problem. I don't know if you've solved the problem now

@qinxianyuzou
Copy link
Author

@shenwenbin123 No, at the moment I can only solve the problem by loading butterknife into the application module.

@wangkunlin
Copy link

wangkunlin commented Dec 9, 2017

@qinxianyuzou 这个不是 bug ,因为 butterknife 的工作机制是:利用 annotationProcessor 来生成 XXX_ViewBinding 类的,对应的 XXX 里面的 View 的初始化是在 XXX_ViewBinding 内完成的。
而 annotationProcessor 是以 module 为单位工作的,所以解决方法很简单,在 app module 内引用 annotationProcessor 而在 library 内引用 butterknife 即可解决。

  @Nullable @CheckResult @UiThread
  private static Constructor<? extends Unbinder> findBindingConstructorForClass(Class<?> cls) {
    Constructor<? extends Unbinder> bindingCtor = BINDINGS.get(cls);
    if (bindingCtor != null) {
      if (debug) Log.d(TAG, "HIT: Cached in binding map.");
      return bindingCtor;
    }
    String clsName = cls.getName();
    if (clsName.startsWith("android.") || clsName.startsWith("java.")) {
      if (debug) Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
      return null;
    }
    try {
      Class<?> bindingClass = Class.forName(clsName + "_ViewBinding");
      //noinspection unchecked
      bindingCtor = (Constructor<? extends Unbinder>) bindingClass.getConstructor(cls, View.class);
      if (debug) Log.d(TAG, "HIT: Loaded binding class and constructor.");
    } catch (ClassNotFoundException e) {
      if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
      bindingCtor = findBindingConstructorForClass(cls.getSuperclass());
    } catch (NoSuchMethodException e) {
      throw new RuntimeException("Unable to find binding constructor for " + clsName, e);
    }
    BINDINGS.put(cls, bindingCtor);
    return bindingCtor;
  }

个人见解,轻喷

@luckbing
Copy link

@qinxianyuzou 大神,问题解决没?我无意间看到其他大神在library里面引用butterknife,首先在project的gradle文件里面的buildscript下面用butterknife作者提供的插件dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
},然后在library的module的gradle里面添加上这两个插件:
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'
,最后在所有@BindView的地方用R2.id.替换掉R.id.,这样就不会空指针了,好像butterknife的readme里面也是这样介绍的,可能大神你没太注意,希望可以帮助到你和其他遇到问题的大神们,好像说是android资源查找的机制导致的,所以jakewharton大神自己提供了一个gradle插件com.jakewharton:butterknife-gradle-plugin:***就是解决这个问题的

@gmm932
Copy link

gmm932 commented Dec 15, 2017

must add annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' in app module to solve this problem.

@om-ganesh
Copy link

om-ganesh commented Apr 8, 2018

Thank you @gmm932 Your suggestion works (probably missed that part from README file)

App level configuration:

dependencies {
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

Project level configuration:

buildscript {
     dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
  }
}

@kaxi4it
Copy link

kaxi4it commented Apr 17, 2018

@qinxianyuzou 最后有搞定吗?在lib中引入黄油刀,在app module中使用
我是as 3.0.1 gradle 3.0.1

@cjiahuan
Copy link

cjiahuan commented Jun 22, 2018

My App also had this problem, this was because I used Glide 4.0 in my app. I could not find viewbinging.class from build/generated/source. For updaeing Glide to 4.0 version, I add code: kapt 'com.github.bumptech.glide:compiler:4.7.1'. Then butterknife 7.0 did not work!!! So, I updated butterknife to 8.0+, also not work!! But, when I changed code annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' to kapt 'com.jakewharton:butterknife-compiler:8.8.1', it worked.

@ximencx
Copy link

ximencx commented Jun 24, 2018

同样的问题 gradle4.1 tools:3.1.0, 跑不起来

@ximencx
Copy link

ximencx commented Jun 24, 2018

有解决的么 lib的build放入依赖 ,,app放注解,也报错

@wangkunlin
Copy link

按着 README.md 里的方法配置就可以了。

@ximencx
Copy link

ximencx commented Jun 25, 2018

@wangkunlin 最后default加了一段代码 还用7.0.1解决,这个框架不建议在项目中用,不如base里抽个findview'byid兼容性好

@jasonSuzhow
Copy link

//used in kotlin
kapt 'com.jakewharton:butterknife-compiler:9.0.0-rc1'

//used in java
annotationProcessor 'com.jakewharton:butterknife-compiler:9.0.0-rc1'

i thought kapt also can used in java, ai yousang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests