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

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




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值