Android Butterknife - 在片段中绑定
Android Butterknife - binding in fragment
我是第一次使用 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;
}
}
无一例外。手动绑定有效,所以我的视图必须在那里。
代码方面,这看起来不错。因此,根据评论,您似乎需要在 Eclipse 中设置注释处理:http://jakewharton.github.io/butterknife/ide-eclipse.html
这对我有用:
Gradle
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");
...
.
完成后别忘了释放:
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();
}
我是第一次使用 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;
}
}
无一例外。手动绑定有效,所以我的视图必须在那里。
代码方面,这看起来不错。因此,根据评论,您似乎需要在 Eclipse 中设置注释处理:http://jakewharton.github.io/butterknife/ide-eclipse.html
这对我有用:
Gradle
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");
...
.
完成后别忘了释放:
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();
}