6

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;
    }
}
2

2 Answers 2

14

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.

3
  • Ok you are right. I didn't accept the answer all this time because I was still getting a null pointer, but for another reason (I was setting the button listener in onCreateView). I apologize for my retardness.. Commented Jan 29, 2013 at 8:08
  • although @ContextView didn't work...do you maybe know if this is possible? Commented Jan 29, 2013 at 11:59
  • I don't think so. Looking at the source of of ContentView, RoboActivity, and ContentViewListener, it appears you're only going to be able to use @ContentView with RoboActivities. Fragments are inflated in their #onCreateView method as you did in the original question.
    – RonU
    Commented Jan 29, 2013 at 21:45
-3

You need to return view. After upBtn.setSelected(false);

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 = (Button) view.findViewById(YOUR_ID); // Initialization
    upBtn.setSelected(false);   // <------ Null pointer here
 return view;
}
3
  • 1
    This won't solve NullPointerException. But you're right, OP has forgotten the return statement in their question (not in his real code).
    – jelies
    Commented Jan 8, 2013 at 19:54
  • 1
    No it doesn't work, sorry. The code actually included the return statement. If it was missing I would get a compilation error. Commented Jan 9, 2013 at 8:45
  • The problem is related to dependency injection (roboguice), not in the code itself. He asked how to inject upBtn view in a fragment. Recently I had the same problem and finished using view.findById(...) but I'm interested in a solution that involves @ContentView(...).
    – jelies
    Commented Jan 9, 2013 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.