Android studio butterknife 注解成功但运行报空指针,求解

心里还是小鲜肉 2016-05-06 11:07:52
compile 'com.jakewharton:butterknife:8.0.1'是最新的
插件也安装完成了





import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class TestActivity extends Activity {


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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);

textView3.setText("不明白");//这行报空指针
}


...全文
15440 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForAuir_ 2018-10-09
  • 打赏
  • 举报
回复
引用 34 楼 bluecountry 的回复:
加这2个就好了
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'



试了好多方法 就这个可以
「已注销」 2017-03-01
  • 打赏
  • 举报
回复
加这2个就好了 compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
qq_35832029 2017-01-04
  • 打赏
  • 举报
回复
8.0以后加两个依赖就可以了 compile 'com.jakewharton:butterknife:8.4.0' compile 'com.jakewharton:butterknife-compiler:8.4.0'
dodod2012 2016-08-04
  • 打赏
  • 举报
回复
引用 31 楼 u012291137 的回复:
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.excellence.netmediaplayer" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } apply plugin: 'com.neenbedankt.android-apt' dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' } http://my.oschina.net/ximidao/blog/671324?fromerr=qcSNhvAH 可以用的
正解
Tiimor 2016-06-06
  • 打赏
  • 举报
回复
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.excellence.netmediaplayer" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } apply plugin: 'com.neenbedankt.android-apt' dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' } http://my.oschina.net/ximidao/blog/671324?fromerr=qcSNhvAH 可以用的
Chace P 2016-06-01
  • 打赏
  • 举报
回复
引用 28 楼 u012810034 的回复:
apply plugin: 'com.android.application' apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.phc.recyclerveiwdemo" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.3.0' compile 'com.android.support:recyclerview-v7:23.3.0' compile 'com.android.support:cardview-v7:23.3.0' compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' } 我加了这三个就行了
除此之外还需要 在整个工程的classpath中加入 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 上次忘了 例如 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 }
xia609676612 2016-05-31
  • 打赏
  • 举报
回复
我用findViewById就不会报错 一开始用注解怎么都报错,后面添加了以下内容就OK了 1、在app的build.gradle中添加以下代码: apply plugin: 'com.neenbedankt.android-apt' compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' 2、在项目中的build.gradle中添加了 dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }
Chace P 2016-05-24
  • 打赏
  • 举报
回复
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

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

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

我加了这三个就行了
  • 打赏
  • 举报
回复
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

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

dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
  • 打赏
  • 举报
回复
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

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

dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
  • 打赏
  • 举报
回复
引用 23 楼 qq_32221419 的回复:
楼主怎么解决的?
我需要的只是注解findviewbyid,所以只是用7.0.1的就足够了 在dependencies { compile 'com.jakewharton:butterknife:7.0.1' } 有时间会去看看8.0.1
jr_T 2016-05-10
  • 打赏
  • 举报
回复
引用 22 楼 u011633245 的回复:
这个更新到8.0.1了,按照github上的文档写就可以了。 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } apply plugin: 'com.neenbedankt.android-apt' dependencies { compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' }
亲测 不管用
jr_T 2016-05-10
  • 打赏
  • 举报
回复
楼主怎么解决的?
辉辉菜 2016-05-08
  • 打赏
  • 举报
回复
这个更新到8.0.1了,按照github上的文档写就可以了。 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } apply plugin: 'com.neenbedankt.android-apt' dependencies { compile 'com.jakewharton:butterknife:8.0.1' apt 'com.jakewharton:butterknife-compiler:8.0.1' }
  • 打赏
  • 举报
回复
好的,可以试试
jklwan 2016-05-07
  • 打赏
  • 举报
回复
引用 19 楼 qq_33819311 的回复:
其他的我就不知道原因了,希望能有人解决 感谢同志们的回答!周末愉快!
去 github主页找使用方式
qq_29637837 2016-05-06
  • 打赏
  • 举报
回复
注意com.zcgd.shop/com.zcgd.TestActivity这里 你看下你的AndroidManifest.xml
  • 打赏
  • 举报
回复
引用 12 楼 qq_29637837 的回复:
你是在线导butterknife-8.0.1还是网站找的butterknife-8.0.1
在Android studio里面直接找的 log Process: com.zcgd.shop, PID: 23664 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zcgd.shop/com.zcgd.TestActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469) at android.app.ActivityThread.access$1100(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5532) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.zcgd.TestActivity.onCreate(TestActivity.java:29)就是设置字体那儿 at android.app.Activity.performCreate(Activity.java:5350) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2381) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2469)  at android.app.ActivityThread.access$1100(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)  at android.os.Handler.dispatchMessage(Handler.java:110)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:5532)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)  at dalvik.system.NativeStart.main(Native Method) 
qq_29637837 2016-05-06
  • 打赏
  • 举报
回复
你是在线导butterknife-8.0.1还是网站找的butterknife-8.0.1
qq_29637837 2016-05-06
  • 打赏
  • 举报
回复
确定你的布局文件和控件id没有问题之后,你可以用findViewById试试然后在判断是不是注解的问题
加载更多回复(15)

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧