Android Butterknife - binding in fragment

51,898

Solution 1

Code-wise, that looks just fine. So based on the comments, it looks like you need to setup the annotation processing in Eclipse: http://jakewharton.github.io/butterknife/ide-eclipse.html

Solution 2

This work for me:

Gradle

compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'

Code

.
...

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

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, view);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    text_input.setText("Lorem Ipsum");
...
.

Solution 3

also dont forget to release when you are finish :

 private Unbinder unbinder;

...

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.finalisation_step_fragment, container, false);
        unbinder = ButterKnife.bind(this, v);
        //initialize your UI

        return v;
    }

...

   @Override public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
Share:
51,898
breakline
Author by

breakline

Updated on September 06, 2020

Comments

  • breakline
    breakline almost 4 years

    I'm using Butterknife for the first time but something must be wrong. I have a fragment and a Listview and a TextView just for testing but Butterknife wont bind my variables:

    public class MyFragment extends Fragment {
    
        @Bind(R.id.resultListView) ListView resultList;
    
        @Bind(R.id.textView1) TextView test;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_my, container, false);
            ButterKnife.bind(this, view);
            System.out.println(resultList); //null
            System.out.println(view.findViewById(R.id.resultListView)); //works
            System.out.println(test); //null
            System.out.println(view.findViewById(R.id.textView1)); //works
            return view;
        }
    
    }
    

    No exception or anything. Manual binding works so my Views must be there.

  • breakline
    breakline over 8 years
    Thanks, this has been done but interestingly the generated folder is empty. I didn't see that but I guess that has something to do with it?
  • Dan Lew
    Dan Lew over 8 years
    What if you do a clean build?
  • breakline
    breakline over 8 years
    Same. I assume the annotation processor never gets called?
  • Dan Lew
    Dan Lew over 8 years
    It sounds like it - perhaps this might be helpful? stackoverflow.com/a/26036146/60261
  • breakline
    breakline over 8 years
    Thanks, my project looked just like that, but now it works after I restarted Eclipse fully. I guess Clean/Rebuild wasnt enough lol. After a restard Eclipse actually generated the files.
  • Someone Somewhere
    Someone Somewhere about 6 years
    I've never done this ever - is it specifically needed for fragments?
  • John Pang
    John Pang over 5 years
    I copied compile 'com.jakewharton:butterknife:8.6.0' from another project but missed the annoationProcesser. Once I added this line and re-sync the project, it works like a a charm. Thanks.
  • wseme
    wseme about 5 years
    Yes, it is specifically needed for fragments since it has a different lifecycle. This is mentioned in the documentation.