Android Butterknife - binding in fragment

AndroidButterknife

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 2 - Android

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();
    }

Solution 3 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbreaklineView Question on Stackoverflow
Solution 1 - AndroidJonathan Nolasco BarrientosView Answer on Stackoverflow
Solution 2 - AndroidDagnogo Jean-FrançoisView Answer on Stackoverflow
Solution 3 - AndroidDan LewView Answer on Stackoverflow