Android Butterknife - binding in fragment
Asked Answered
V

3

51

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.

Vange answered 11/12, 2015 at 17:0 Comment(4)
What do your dependencies look like?Rippy
I just have the Butterknife jar included.Vange
ahh, so are you using eclipse?Rippy
Yes, unfortunately this project hasnt been updated yet.Vange
E
19

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

Evite answered 11/12, 2015 at 17:15 Comment(5)
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?Vange
What if you do a clean build?Evite
Same. I assume the annotation processor never gets called?Vange
It sounds like it - perhaps this might be helpful? https://mcmap.net/q/354783/-eclipse-doesn-39-t-generate-the-apt_generated-folder-for-butter-knifeEvite
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.Vange
G
62

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");
...
.
Gaseous answered 27/2, 2017 at 2:13 Comment(1)
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.Lejeune
I
24

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();
    }
Institutionalism answered 5/9, 2017 at 14:29 Comment(2)
I've never done this ever - is it specifically needed for fragments?Amu
Yes, it is specifically needed for fragments since it has a different lifecycle. This is mentioned in the documentation.Glottal
E
19

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

Evite answered 11/12, 2015 at 17:15 Comment(5)
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?Vange
What if you do a clean build?Evite
Same. I assume the annotation processor never gets called?Vange
It sounds like it - perhaps this might be helpful? https://mcmap.net/q/354783/-eclipse-doesn-39-t-generate-the-apt_generated-folder-for-butter-knifeEvite
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.Vange

© 2022 - 2024 — McMap. All rights reserved.