Android Butterknife - binding in fragment

前端 未结 3 2015
半阙折子戏
半阙折子戏 2021-02-11 12:19

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:

相关标签:
3条回答
  • 2021-02-11 13:03

    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");
    ...
    .
    
    0 讨论(0)
  • 2021-02-11 13:10

    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

    0 讨论(0)
  • 2021-02-11 13:11

    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();
        }
    
    0 讨论(0)
提交回复
热议问题