ButterKnife.bind(this)发生NullPointerException

简介: 最近做项目,想用黄油刀,刚开始看了写官方的文档,好简单啊,直接把库compile进来,上去就是@BindView(R.id.tx) TextView tv,当我在setContentView里面去set这个TextView的值的时候报空指针异常了,我怎么想怎么不对,然后用@onClick去给TextView设置监听也不管用,感觉这个框架没什么用处啊,也是害苦了,还是去官网再好

最近做项目,想用黄油刀,刚开始看了写官方的文档,好简单啊,直接把库compile进来,上去就是@BindView(R.id.tx) TextView tv,当我在setContentView里面去set这个TextView的值的时候报空指针异常了,我怎么想怎么不对,然后用@onClick去给TextView设置监听也不管用,感觉这个框架没什么用处啊,也是害苦了,还是去官网再好好看看,终于看到自己的坑了,单单去compile一个库是完全没有用的

官方导入图

这里写图片描述

需要添加buildscript{ }、apply plugin: ‘android-apt’还有 apt ‘com.jakewharton:butterknife-compiler:8.4.0’。单单compile一点用都没

我的图

这里写图片描述

ok,再不会报空指针异常了

用法

  在初始化的时候  
  ButterKnife.bind(this);
  成员变量
  @BindView(R.id.main_home)
    ImageView imgHome;
  添加单击事件
 @OnClick(R.id.submit) void submit() {
    // TODO call server...
  }
添加多个单击事件
 @OnClick({R.id.main_home, R.id.main_car, R.id.main_square, R.id.main_msg, R.id.main_my})
    public void onClick(View v) {
        switch (v.getId()){
          case R.id.main_home:
            //do something
            break;
     }       
}

好了,最近没做应用,博客一直都停着没写,接下来估计就是忙的时候了,遇到问题总结后再来博客分享,继续代码生活

目录
相关文章
|
7月前
new bind apply call instanceof 等笔记
new bind apply call instanceof 等笔记
17 0
|
Web App开发 XML Java
java.lang.IllegalStateException: Async support must be enabled on a servlet and for all filters invo
Spring MVC 项目在使用 DeferredResult 实现异步接口时出现错误。 完整错误信息如下: 十一月 03, 2017 8:59:53 上午 org.apache.catalina.
2085 0
|
10月前
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
93 1
|
11月前
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionVie
|
Java 数据库 Spring
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
PowerMock - java.lang.RuntimeException: test should never throw an exception to this level
PowerMock - java.lang.RuntimeException: test should never throw an exception to this level
640 0
PowerMock - java.lang.RuntimeException: test should never throw an exception to this level
Error creating bean with name ‘org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0‘
Error creating bean with name ‘org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0‘
105 0
|
XML 数据格式
使用SearchView报错java.lang.UnsupportedOperationException: Failed to resolve ...
SearchView是appcompat-v7中的一个组件,能够更快的实现一些基本的功能 但是在布局中使用android.support.v7.widget.SearchView,打开该页面会crash,查看错误是加载布局出问题了
733 0
ES6—26:bind方法应用
ES6—26:bind方法应用
115 0
ES6—26:bind方法应用