ButterKnife使用
这个快速注解插件非常好用,但是要注意,本人在用的时候用它去onclick的时候报空指针异常下面说说怎么用。
1.在file里面选择
搞定之后,还需要在APP下的build.gradle
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' //加这一句 android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.administrator.myapplication0022" minSdkVersion 15 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(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.2.0' testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1'//加这一句
}
,另外在项目的build.gradle里面
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.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() } } task clean(type: Delete) { delete rootProject.buildDir }
这些都做完之后就可以快乐的使用注解和快速生成点击监听了,但有时候会generate会不出现butterknife这个插件选项,需要在File -> settings -> Plugins -> Browse repositories -> 搜索ButterKnife,安装完成后restart,重启studio就可以用了