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

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

目录
相关文章
|
8月前
|
搜索推荐 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 问题处理
1362 1
|
5月前
|
Android开发
解決Android报错:Could not initialize class org.codehaus.groovy.reflection.ReflectionCache
解決Android报错:Could not initialize class org.codehaus.groovy.reflection.ReflectionCache
147 1
|
5月前
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
78 1
|
7月前
|
Java 数据库连接 数据库
Error:Internal error: (java.lang.NoClassDefFoundError) Could not initialize class com.intellij.util.
Error:Internal error: (java.lang.NoClassDefFoundError) Could not initialize class com.intellij.util.
|
8月前
|
Java Spring
java.lang.Exception: Method a() should be public
java.lang.Exception: Method a() should be public
89 0
|
8月前
|
Java 应用服务中间件
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallablePro
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallablePro 错误处理
116 0
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
swagger遇到的错误记录java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.anno
251 1
|
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
786 0
PowerMock - java.lang.RuntimeException: test should never throw an exception to this level
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process
java.lang.RuntimeException: Unable to create application com.ads.app.MyApplication: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.ads.app. Make sure to call FirebaseApp.initializeApp(Context) first.
684 0
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process