ButterKnife使用之Activity与Fragment

在Fragment中使用ButterKnife时:

一直出现被绑定的id仍然为null,很是郁闷,之前一直也没留意.........后来发现Activity和Fragment使用上有点区别


Activity中绑定:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setActivityContentView();
    //绑定activity
    //Fragment中绑定返回的View
    ButterKnife.bind(this);

Fragment中绑定:

 
  
private Unbinder unbinder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = setFragmentLayoutInFlater(inflater,container);
    //返回一个Unbinder值(进行解绑),注意这里的this不能使用getActivity()
    unbinder = ButterKnife.bind(this, view);
    return view;
}

/**
 * onDestroyView中进行解绑操作
 */
@Override 
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}









你可能感兴趣的:(Android,ButterKnife,BindView为null,Fragment)