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;
     }       
}

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

目录
相关文章
|
11月前
|
搜索推荐 Java 开发者
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
【5月更文挑战第14天】org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
2157 1
|
8月前
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent or ancestor Con
104 1
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
331 1
|
Java
如何优雅的避免Java中:NullPointerException(空指针异常)
如何优雅的避免Java中:NullPointerException(空指针异常)
162 0
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState(已解决)
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
822 0
PowerMock - java.lang.RuntimeException: test should never throw an exception to this level
|
vr&ar
错误解决:Caused by: java.lang.IllegalArgumentException: The servlets named [loginServlet] and [login] ar
错误解决:Caused by: java.lang.IllegalArgumentException: The servlets named [loginServlet] and [login] ar
471 0
|
XML 数据格式
使用SearchView报错java.lang.UnsupportedOperationException: Failed to resolve ...
SearchView是appcompat-v7中的一个组件,能够更快的实现一些基本的功能 但是在布局中使用android.support.v7.widget.SearchView,打开该页面会crash,查看错误是加载布局出问题了
883 0
|
Java Spring
default constructor not found 异常解决方法
default constructor not found 异常解决方法
2318 0
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI...
这个报错比较骚,完全只是因为Intent没有写好而已,下面是错误的写法(最好不要在方法外赋值): 进入本活动时即刻报错: 11-10 18:23:24.
6823 0