问题
When i'm trying to do this:
...
public class LoginActivity extends AppCompatActivity {
@BindView(R.id.login_form) View loginForm;
...
loginForm
is getting null
. I tried to follow other answers and nothing worked (this for example). I also did exactly what it said in the butterKnife configuration page and it didn't work. What am I doing wrong?
module gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "name"
minSdkVersion 22
targetSdkVersion 25
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.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'uk.co.chrisjenx:calligraphy:2.3.0'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Project gradle:
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
回答1:
Probably, you forget to put this line:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
...
}
回答2:
In onCreate you have to call
ButterKnife.Bind(this)
before you use the views
Also add mavenCentral() to repositories in gradle
repositories {
jcenter()
mavenCentral()
}
来源:https://stackoverflow.com/questions/44605871/butterknife-returns-null-when-binding-view-8-6-0