我在做什么 ::
MainActivity.java
package com.example.butterknife;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends Activity {
@InjectView(R.id.txtID) TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
title.setText("Hello Everyone !!");
}
}
日志 ::
04-15 11:50:57.845: E/AndroidRuntime(913): FATAL EXCEPTION: main
04-15 11:50:57.845: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.butterknife/com.example.butterknife.MainActivity}: java.lang.NullPointerException
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.os.Looper.loop(Looper.java:137)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-15 11:50:57.845: E/AndroidRuntime(913): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 11:50:57.845: E/AndroidRuntime(913): at java.lang.reflect.Method.invoke(Method.java:511)
04-15 11:50:57.845: E/AndroidRuntime(913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-15 11:50:57.845: E/AndroidRuntime(913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-15 11:50:57.845: E/AndroidRuntime(913): at dalvik.system.NativeStart.main(Native Method)
04-15 11:50:57.845: E/AndroidRuntime(913): Caused by: java.lang.NullPointerException
04-15 11:50:57.845: E/AndroidRuntime(913): at com.example.butterknife.MainActivity.onCreate(MainActivity.java:19)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.Activity.performCreate(Activity.java:4465)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-15 11:50:57.845: E/AndroidRuntime(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-15 11:50:57.845: E/AndroidRuntime(913): ... 11 more
04-15 11:51:06.722: I/Process(913): Sending signal. PID: 913 SIG: 9
答案 0 :(得分:13)
按照以下步骤[进入Eclipse IDE]:
1)在包资源管理器中右键单击您的项目。得到Java Compiler
- > Annotation Processing
并查看Enable project specific settings
。
2)展开Annotation Processing
部分,然后选择Factory Path
。检查Enable project specific settings
,然后点击Add JARs
导航到项目的libs /文件夹,然后选择ButterKnife
jar。
3)点击Ok
以保存新设置。 Eclipse
会要求您重建您应该点击Yes
。
注意:确保.apt_generated/
文件夹位于项目根目录中。它应包含YOURACTIVITY$$ViewInjector.java
等文件。如果这些文件不存在,则通过选定的项目→清除触发清洁构建。不应将此文件夹和文件签入修订控件。
参考 - > Set up manual configuration for ButterKnife annotations
答案 1 :(得分:3)
IntelliJ IDEA - 如果您的项目使用外部配置(如 Maven pom.xml)然后注释处理应该正常工作。
请按照以下步骤操作:
File
- > Other Settings
- > Default Settings
Compiler
- > Annotation Processors
然后检查Enable annotation processing
参考 - > Set up manual configuration for ButterKnife annotations
更新30-6-2015
对Intellij
IDEA 14.0:
打开IDEA的偏好设置:File
- > Settings
或点击ctl + alt + s
导航至Build, Execution and deployment
- > Compiler
- > Annotation Processors
并选择您的项目然后选中Enable annotation processing
答案 2 :(得分:0)
@Bind取代@InjectView和@InjectViews。 ButterKnife.bind和ButterKnife.unbind分别取代了ButterKnife.inject和ButterKnife.reset。