绑定视图时,butterKnife返回null(8.6.0)

时间:2017-06-17 14:39:21

标签: android android-studio butterknife

当我试图这样做时:

...
public class LoginActivity extends AppCompatActivity {
@BindView(R.id.login_form) View loginForm;
...

loginForm正在获得null。我试图遵循其他答案而没有任何效果(例如这个)。我也完成了它在butterKnife配置页面中所说的内容并且它不起作用。我做错了什么?

模块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'

2 个答案:

答案 0 :(得分:1)

在onCreate中,您必须致电

ButterKnife.Bind(this) 

使用视图之前

还将mavenCentral()添加到gradle中的存储库

repositories {
    jcenter()
    mavenCentral()
    }

答案 1 :(得分:1)

可能你忘记了这一行:

@Override 
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.simple_activity);

   ButterKnife.bind(this);
   ...
}