Using Butter Knife library for view injection

10,294

Solution 1

Follow this Steps [ into Eclipse IDE ]:

1)Right click on your project withIn Package Explorer. Got to Java Compiler --> Annotation Processing and check the Enable project specific settings.

2)Expand the Annotation Processing section and select Factory Path. Check Enable project specific settings and then click Add JARs Navigate to the project's libs/ folder and select the ButterKnife jar.

3)Click Ok to save the new settings. Eclipse will ask you to rebuild your project to which you should click Yes.

Note: Make sure that the .apt_generated/ folder is in your project root. It should contain files like YOURACTIVITY$$ViewInjector.java. If these files are not present trigger a clean build by selected Project → Clean. This folder and files should not be checked into revision control.

Reference -> Set up manual configuration for ButterKnife annotations

Solution 2

IntelliJ IDEA — If your project uses an external configuration (like a Maven pom.xml) then annotation processing should just work.

Follow these steps :

  1. Open IDEA's preference: File -> Other Settings -> Default Settings
  2. Navigate to Compiler -> Annotation Processors Then Check Enable annotation processing

Reference -> Set up manual configuration for ButterKnife annotations

Update 30-6-2015

Follow These Steps for Intellij IDEA 14.0 :

  1. Open IDEA's preference: File -> Settings or click ctl + alt + s

  2. Navigate to Build, Execution and deployment -> Compiler -> Annotation Processors and Choose your project Then Check Enable annotation processing

Share:
10,294
Devrath
Author by

Devrath

I work as a mobile Applications developer and specialize in Android and Flutter I learn by Sharing my knowledge and learning from others I'm a curious creature and like staying up to date with new technologies My Global stack-overflow ranking

Updated on June 07, 2022

Comments

  • Devrath
    Devrath about 2 years

    What i am doing::

    1. I am trying to run butter knife library for my simple project
    2. I have followed all the steps in documentation, but still i am getting log errors
    3. How can i resolve this, am i missing any step
    4. I have also added the jar in libs folder

    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 !!");
    
        }
    }
    

    Log::

    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
    
    • Anantha Sharma
      Anantha Sharma about 10 years
      looks like title is null, it hasn't been injected with anything.
  • Devrath
    Devrath about 10 years
    Thanks for pointing me on the Documentation ... I was able to resolve it [+1] too !
  • Nitesh Tiwari
    Nitesh Tiwari about 10 years
    @Casper Love to Help :)
  • mbwasi
    mbwasi over 9 years
    The Annotation Processing option didn't exist in my Eclipse installation had to follow the work around here stackoverflow.com/a/26028701/329034
  • RenniePet
    RenniePet about 9 years
    It may be better to use File - Settings instead of File - Default Settings. And for IntelliJ IDEA 14.1 the settings dialog has gotten one level deeper, so it's Build, Execution, Deployment - Compiler - Annotation Processors.
  • IgorGanapolsky
    IgorGanapolsky almost 9 years
    There is no such thing as Java Compiler there.
  • IgorGanapolsky
    IgorGanapolsky almost 9 years
    It isn't an option anymore.
  • ahmed hamdy
    ahmed hamdy almost 9 years
    @IgorGanapolsky open Compiler tab, and choose Annotation Processors . Another Way: open Setting Dialog and search for Annotation Processors
  • Nitesh Tiwari
    Nitesh Tiwari almost 9 years
    @IgorGanapolsky This is for eclipse.For studio check this out. tools.android.com/tech-docs/support-annotations
  • IgorGanapolsky
    IgorGanapolsky almost 9 years
    @ahmedhamdy Unfortunately this is not an option in the latest Canary build of Android Studio.
  • Solace
    Solace almost 9 years
    Not working for me =( I followed the steps in Eclipse, but from the Note: "Make sure that the .apt_generated/ folder is in your project root..." - I do not see any folder named apt-generated in my project root, and the errors don't go away.