I've a roboactivity where I inject some stuff like resources and views. I use some fragment classes in my app. These fragments have to extend Fragment. Here is my stuff:
When a button is pressed I make a new fragment:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame,Fragment.instantiate(this,fragments[position]));
fragmentTransaction.commit();
My fragment looks like this:
public class MyLists extends Fragment {
@InjectView(R.id.myview)
private TextView myview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.my_lists_fragment, null);
MyRepo repo= new MyRepo ();
myview.setText(repo.getSomething());
return root;
}
}
The InjectView isn't working. I can't find the reason why it wont work. Can anybody help me solving this?
Injection happens during onViewCreated
which is after onCreateView
. Move your code to onViewCreated
.
On an Activity
there is setContentView
where injection can take place. On a fragment, you return the view so robojuice can't know to use that for injection until later.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.my_lists_fragment, null);
// Here you've inflated something, but robojuice has no way of knowing
// that's the fragments view at this point, and no base method has been called
// that robojuice might use to find your View.
// So myview is still null here:
myview.setText(repo.getSomething());
return root;
}