我第一次使用Butterknife,但一定是错的.我有一个片段,Listview和TextView只是为了测试,但Butterknife不会绑定我的变量:
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; } }
没有例外或任何事情.手动绑定有效,所以我的意见必须在那里.
这项工作对我来说:
摇篮
compile 'com.jakewharton:butterknife:8.6.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
码
. ... @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"); ... .
代码方面,看起来很好.所以基于这些注释,看起来你需要在Eclipse中设置注释处理:http://jakewharton.github.io/butterknife/ide-eclipse.html
当你完成时也不要忘记释放:
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(); }