Butterknife: R2类在library module中并不能用

Created on 9 Oct 2016  ·  30Comments  ·  Source: JakeWharton/butterknife

help me!!!完全按照例子来写的,但是就是不起作用,R2文件能正常生成,但是BindView后使用View报空指针!

Most helpful comment

I do not want to be rude, but could you please use at least try to use english?

I think I might have the same issue, but your comments and error description is rather useless here.

I experienced issues when upgrading from butterknife 8.2.1 -> 8.4.0 in my library project.
Although the Activity_ViewBinding.java class seems okay, the view cannot be bound correctly and produces a null pointer exception.

This is probably the same issue u guys are experiencing, but your comments are not helping.

All 30 comments

不使用“R2”认真只要按照这个链接,而不是


dont use "R2" seriously just follow this link instead
http://jakewharton.github.io/butterknife/

I got the same problem, somebody help us?

@Jungle68
the link I provided should have pointed you in the right direction....

@Xstar97
but R.id in library module isn't constant, so bindView can't be used

project level gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.google.gms:google-services:3.0.0'
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
}
}

allprojects {
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
maven {url = "http://dl.bintray.com/davideas/maven" }
maven {url = "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
_
module level gradle:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.mcs.example"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'

compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'

testCompile 'junit:junit:4.12'

}
//optional
//apply plugin: 'com.google.gms.google-services'

_
Activity:

public class MainActivity extends AppCompatActivity {

@BindString(R.string.random) String String_Random;
@BindView(R.id.randomTextView) TextView randomTextView;
@BindView(R.id.toolbar) Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

   randomTextView.setText(String_Random);

}
__
can this example suffice?

@Xstar97
It's not work.
@Joy-Whale
Can you solve this problem?

@Jungle68
How are you utilizing the library?
"It's not work" is a very generic statement..can please provide an error report/logs/anything?

@Xstar97
R2 file generated properly, but after the BindView, use the View to null pointer!

@Jungle68 表示没解决,完全按例子来也不行啊

@Xstar97 I means I used butterKnife in a library module
image
like this,your example is in an application module, so it is work

但是JakeWharton ,给了示例是可以的哇,太痛苦了,不知道怎么弄了。
@Joy-Whale 那你怎么解决的,全部重新用findViewById?

@Jungle68 是的,按照例子来 能生成R2文件,但还是不能用,估计和你情况是一样的,现在只能是findViewById搞了,幸运的是有个快速生成工具和ButterKnife那个工具一样好用,要不得疯

@Joy-Whale
什么工具?推荐一下呗

@Jungle68
Android View Generator

@Joy-Whale 这个是完全重新生成Activty这些?我现在是把主项目的东西移植到Module中,用不了诶

@Jungle68
不是的 有两个 一个是Code Generator 一个是View Generator,你说的是Code Generator吧,View Generator用法和butterKnife一样 右键layout ID - Generate - Generate view code

@Joy-Whale 谢谢,我试试

I do not want to be rude, but could you please use at least try to use english?

I think I might have the same issue, but your comments and error description is rather useless here.

I experienced issues when upgrading from butterknife 8.2.1 -> 8.4.0 in my library project.
Although the Activity_ViewBinding.java class seems okay, the view cannot be bound correctly and produces a null pointer exception.

This is probably the same issue u guys are experiencing, but your comments are not helping.

@xMikeTx
For the love of God.... just translate it yourself with translate.google.com

@xMikeTx
Also did you try my example that I posted here too?

@xMikeTx
Yes, we are the same issus!

@Xstar97 Relax. And yes I tried your example and it works, as does my project when I use butterknife for my application.

But when I try to use butterknife for my library project, which is supported since 8.x.x, it does not work with the latest update (apply plugin: 'com.android.library')
-> here also comes the thing into play where R2.id should be used.

有任何你试图创建一个具有id列表的XML文件的视图使用&然后声明R.id.xxx而不是R2.id.xxx


Have any of you tried to create an XML file with a list of id's for the views to use & then declare R.id.xxx instead of R2.id.xxx?

I got this problem.

Project:Module -> Gradle
buildscript {
repositories {
//jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
}
}
allprojects {
repositories {
mavenCentral()
}
}


the library -> gradle
apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.fresco:fresco:0.12.0'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

}

app module -> Gradle
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
    applicationId "com.sicmu.lovefate"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 3
    versionName "1.0.2"


}

sourceSets {
    main {
        jniLibs.srcDirs = ['libs']
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/upload_1.1.3.300_4200.jar')
compile files('libs/wup-1.0.0-SNAPSHOT.jar')
compile files('libs/MobTools.jar')
compile files('libs/MobCommons.jar')
compile name: 'SMSSDK-2.0.1', ext: 'aar'
compile project(':im')
compile 'com.tencent.bugly:crashreport_upgrade:latest.release'
compile 'com.tencent.bugly:nativecrashreport:latest.release'
compile files('libs/AMap_Location_V3.2.0_20161205.jar')
compile files('libs/AMap_Search_V3.6.1_20161122.jar')
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

}

in the library, i define a class:
public abstract class BaseFragment extends Fragment {
private Unbinder unbinder;

/**
 * 主界面容器
 */
private ViewGroup mLayoutContent;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_layout, container, false);
    mLayoutContent = (ViewGroup) layout.findViewById(R.id.layout_content);
    int res = obtainCreateView();
    if (res != -1) {
        inflater.inflate(res, mLayoutContent, true);
    }
    ButterKnife.setDebug(true);
    unbinder = ButterKnife.bind(this, layout);
    componentInit(layout);
    return layout;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}

/**
 * 获取布局资源
 *
 * @return
 */
protected int obtainCreateView() {
    return -1;
}

}

and in the library, i define another class
public class BaseListFragment extends BaseFragment implements
SwipeRefreshLayout.OnRefreshListener,
BaseAdapter.OnItemClickListener,
BaseAdapter.OnItemLongClickListener {

/**
 * 下拉刷新组件
 */
@BindView(R2.id.swipe)
protected SwipeRefreshLayout swipe;

/**
 * 列表组件
 */
@BindView(R2.id.list)
protected RecyclerView recycler;

/**
 * 设置Fragment的布局
 *
 * @return 返回布局id
 */
protected int obtainCreateView() {
    return R.layout.fragment_base_list_layout;
}

@Override
public void componentInit(View view) {
    super.componentInit(view);
    // 设置布局管理器
    recycler.setLayoutManager(obtainLayoutManager());
    // 设置divider
    RecyclerView.ItemDecoration decoration = obtainDecoration();
    if (decoration != null) {
        recycler.addItemDecoration(decoration);
    }
    // 设置适配器
    mAdapter = obtainAdapter();
    recycler.setAdapter(mAdapter);
}

.....

}

and the problem is :
when i define a class HomeFragment in the app model, and extends BaseListFragment,
recycler defined in BaseFragment is null ,and throw the NullPointerException.

我遇到了这个问题,搞了我好久。
你们仔细看看布局文件名是不是有相同!!!
别问我是怎么知道的 /(ㄒoㄒ)/~~

@zhangli915952504 You're awesome!

我的AS是3.3.2版本,在library中使用butterknife,@BindView使用的是R2,但是R2一直报红,Cannot resolve symbol ‘R2’,clean、rebuild等一些操作都没有用,但是代码能够跑起来,APP也能正常运行。但是这个R2报红会影响我正常的错误定位,目前还不知道怎么处理,希望牛人可以解答

我的AS是3.3.2版本,在library中使用butterknife,@BindView使用的是R2,但是R2一直报红,Cannot resolve symbol ‘R2’,clean、rebuild等一些操作都没有用,但是代码能够跑起来,APP也能正常运行。但是这个R2报红会影响我正常的错误定位,目前还不知道怎么处理,希望牛人可以解答

@wy521angel 升级butterknife版本就行,见https://www.cnblogs.com/bellkosmos/p/10680595.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZakTaccardi picture ZakTaccardi  ·  3Comments

engr-umar-qureshi picture engr-umar-qureshi  ·  3Comments

alejandro-pnz picture alejandro-pnz  ·  3Comments

hwra2008 picture hwra2008  ·  3Comments

Pitel picture Pitel  ·  3Comments