AndroidStudio 依赖 ButterKnife 出现的空指针异常

问题:AndroidStudio 依赖 ButterKnife 出现的空指针异常
解决:ButterKnife配置

带*号为添加的代码
app/build.gradle

apply plugin: 'com.android.application'
**apply plugin: 'com.neenbedankt.android-apt'**

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.eminvoker.mvpdemo"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    **compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'**
}

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.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
}

代码中使用:(xml中有一个TextView id为tx,一个Button id为btn)

    @BindView(R.id.tx)
    TextView tx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tx.setText("Hello");
    }


    @OnClick(R.id.btn)
    public void onClick() {   
       tx.setText("goodbye")    
    }

更多用法看官方文档,这里只是随便喵喵
为了方便将xml的控件绑定到Activity中,下载插件:Android ButterKnife Zelezny
安装插件后点击Activity中的xml布局文件选择生成绑定代码(吐槽小唧唧)
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值