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
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RoboGuice.getInjector(getActivity()).injectViewMembers(this);
}
If you insist on injecting manually, use injectViewMembers(). If you can delay touching the view until after onViewCreated(), then it will be set up for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With