Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InjectView in RoboFragment

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;
    }
}
like image 716
Thomas Kaliakos Avatar asked Jan 08 '13 07:01

Thomas Kaliakos


1 Answers

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.

like image 182
RonU Avatar answered Nov 15 '22 03:11

RonU