Android ButterKnife 的安装使用和控件为Null处理

来源:互联网 发布:sql server 2008 免费 编辑:程序博客网 时间:2024/05/01 04:59

File –> Settings –> Plugins –> Browse repositories


查找ButterKnife插件,安装


重启Android Studio

使用ButterKnife插件

添加jar包

  • 右键Module,Open Module Settings

  • 切换到Dependencies,点击右侧的“+”,选择“Library Dependency”


搜索ButterKnife,添加jar包


或者直接在build.gradle里面的

dependencies


下面添加

compile 'com.jakewharton:butterknife:8.7.0'compile 'com.jakewharton:butterknife-compiler:8.7.0'

生成注解

  • 右键layout的ID,点击“Generate”


选择“Generate Butterknife Injections”



生成注解

注意事项如下:



如箭头所见build.gradle中添加

dependencies{
//butterknife注解compile 'com.jakewharton:butterknife:8.7.0'compile 'com.jakewharton:butterknife-compiler:8.7.0'
在build.gradle顶部添加:apply plugin: 'com.android.application'apply plugin: 'com.neenbedankt.android-apt'//依赖butterKnife在第二个build.gradle里面加入
dependencies {    classpath 'com.android.tools.build:gradle:2.3.3'    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'//butterknife防止为null    // NOTE: Do not place your application dependencies here; they belong    // in the individual module build.gradle files}在acticvity 或者 fragment 里面使用时注意:
unbinder = ButterKnife.bind(this, view);
tvTitle.setText("gsdgg");//绑定控件要在unbinder下面否则报
tvTitle
为null




原创粉丝点击