I want to inject some views from an xml layout to a RoboFragment but unfortunately I am getting Nullpointer Exception. Since RoboGuice (besides being a great DI framework) has very little documentation, I don’t know if I can use @ContentView(R.layout.fragmentlayout) to annotate my RoboFragment. Is there something I should do instead? What I currently do is:
public class TestFragment extends RoboFragment {
@InjectView(R.id.upBtn) private Button upBtn;
@Override
public View onCreateView(LayoutInflater layoutInflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(layoutInflater, container, savedInstanceState);
View view = layoutInflater.inflate(R.layout.fragmentlayout, container, false);
RoboGuice.getInjector(getActivity()).injectMembers(this);
upBtn.setSelected(false); // <------ Null pointer here
return view;
}
}
If you look at the source for RoboFragment, you’ll see
If you insist on injecting manually, use
injectViewMembers(). If you can delay touching the view until afteronViewCreated(), then it will be set up for you.