butterKnife returns null when binding view (8.6.0)

六月ゝ 毕业季﹏ 提交于 2021-02-05 09:13:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!