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;
    }
}
Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
  • I hope you took a look to these two questions: http://stackoverflow.com/questions/9780769/android-roboguice-inject-views-on-fragment and http://stackoverflow.com/questions/8289660/any-simple-examples-using-roboguice-with-fragments-in-android – Eugen Martynov Jan 14 '13 at 11:31
  • Yes. The first one just points out that the injection happens during onViewCreated but I don't unsterstand how. In the second one I also tried RoboGuice.getInjector(getActivity()).injectViewMembers(this); but it didn't help. – Thomas Kaliakos Jan 14 '13 at 12:35

2 Answers2

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.

RonU
  • 5,525
  • 3
  • 16
  • 13
  • 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.. – Thomas Kaliakos Jan 29 '13 at 08:08
  • although @ContextView didn't work...do you maybe know if this is possible? – Thomas Kaliakos Jan 29 '13 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 Jan 29 '13 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;
}
Tsunaze
  • 3,204
  • 7
  • 44
  • 81
  • 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 Jan 08 '13 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. – Thomas Kaliakos Jan 09 '13 at 08: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 Jan 09 '13 at 08:54