使用Butter Knife库进行视图注入

时间:2014-04-15 06:28:11

标签: android butterknife

我在做什么 ::

  1. 我正在尝试为我的简单项目运行butter knife library
  2. 我已经按照文档中的所有步骤进行了操作,但我仍然是 获取日志错误
  3. 我如何解决这个问题,我错过了任何一步
  4. 我还在libs文件夹中添加了jar

  5. 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
    

3 个答案:

答案 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)然后注释处理应该正常工作。

请按照以下步骤操作:

  1. 打开IDEA的偏好:File - > Other Settings - > Default Settings
  2. 导航至Compiler - > Annotation Processors然后检查Enable annotation processing
  3. 参考 - > Set up manual configuration for ButterKnife annotations

    更新30-6-2015

    Intellij IDEA 14.0:

    执行以下步骤
    1. 打开IDEA的偏好设置:File - > Settings或点击ctl + alt + s

    2. 导航至Build, Execution and deployment - > Compiler - > Annotation Processors并选择您的项目然后选中Enable annotation processing

答案 2 :(得分:0)

@Bind取代@InjectView和@InjectViews。 ButterKnife.bind和ButterKnife.unbind分别取代了ButterKnife.inject和ButterKnife.reset。

看看这个:@InjectView replaced ...