butterknife icon indicating copy to clipboard operation
butterknife copied to clipboard

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

Open qinxianyuzou opened this issue 7 years ago • 34 comments

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

qinxianyuzou avatar Oct 30 '17 03:10 qinxianyuzou

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?

JakeWharton avatar Oct 30 '17 04:10 JakeWharton

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

qinxianyuzou avatar Oct 30 '17 04:10 qinxianyuzou

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 avatar Oct 30 '17 06:10 imaiya

@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遇到这个问题,很烦

qinxianyuzou avatar Oct 30 '17 07:10 qinxianyuzou

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

imaiya avatar Oct 30 '17 07:10 imaiya

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

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

qinxianyuzou avatar Oct 30 '17 08:10 qinxianyuzou

@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.

ranjitzade avatar Oct 30 '17 13:10 ranjitzade

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

wvisible avatar Oct 31 '17 08:10 wvisible

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

qinxianyuzou avatar Oct 31 '17 08:10 qinxianyuzou

@qinxianyuzou yes,you can try it.

wvisible avatar Oct 31 '17 08:10 wvisible

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

imaiya avatar Oct 31 '17 08:10 imaiya

@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

qinxianyuzou avatar Oct 31 '17 09:10 qinxianyuzou

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

wangyuetingtao avatar Oct 31 '17 10:10 wangyuetingtao

you can add

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

in app module build.gradle too.

wimwu avatar Nov 01 '17 08:11 wimwu

@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.

qinxianyuzou avatar Nov 03 '17 04:11 qinxianyuzou

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

Drakot avatar Nov 07 '17 10:11 Drakot

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

nirmal912 avatar Nov 07 '17 11:11 nirmal912

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

fauzanmakky avatar Nov 08 '17 08:11 fauzanmakky

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.

BansookNam avatar Nov 08 '17 09:11 BansookNam

@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

kassim avatar Nov 08 '17 09:11 kassim

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

BansookNam avatar Nov 08 '17 09:11 BansookNam

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

shenwenbin123 avatar Nov 22 '17 02:11 shenwenbin123

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

qinxianyuzou avatar Nov 22 '17 08:11 qinxianyuzou

@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;
  }

个人见解,轻喷

wangkunlin avatar Dec 09 '17 06:12 wangkunlin

@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:***就是解决这个问题的

luckbing avatar Dec 12 '17 02:12 luckbing

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

gmm932 avatar Dec 15 '17 03:12 gmm932

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'
  }
}

om-ganesh avatar Apr 08 '18 03:04 om-ganesh

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

kaxi4it avatar Apr 17 '18 08:04 kaxi4it

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.

cjiahuan avatar Jun 22 '18 05:06 cjiahuan

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

ximencx avatar Jun 24 '18 17:06 ximencx

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

ximencx avatar Jun 24 '18 17:06 ximencx

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

wangkunlin avatar Jun 25 '18 02:06 wangkunlin

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

ximencx avatar Jun 25 '18 15:06 ximencx

//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

jasonSuzhow avatar Oct 18 '18 09:10 jasonSuzhow