2017-06-17 89 views
0

時,當我試圖做到這一點:butterKnife返回null結合視圖(8.6.0)

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

項目gradle這個:

dependencies { 
    classpath 'com.android.tools.build:gradle:2.3.3' 
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0' 
+0

安置自己的onCreate方法,並在那裏你有login_form的XML。 –

+0

選中此行添加「ButterKnife.bind(this);」在onCreate方法中。 –

回答

1

在的onCreate你必須調用

ButterKnife.Bind(this) 

您使用的意見

另外補充mavenCentral(前)t Ø在gradle這個

repositories { 
    jcenter() 
    mavenCentral() 
    } 
1

庫也許,你忘了把這一行:

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

    ButterKnife.bind(this); 
    ... 
}