我有一个机器人活动,我注入了一些东西,比如资源和视图。我在我的应用程序中使用了一些片段类。这些碎片必须扩展碎片。这是我的东西:
当按下一个按钮时,我会做一个新的片段:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame,Fragment.instantiate(this,fragments[position]));
fragmentTransaction.commit();我的碎片是这样的:
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;
}
}InjectView不起作用。我找不到它不起作用的原因。有人能帮我解决这个问题吗?
发布于 2014-05-24 16:36:42
注入发生在onViewCreated期间,也就是onCreateView之后。将代码移动到onViewCreated。
在Activity上,可以在setContentView上进行注入。在一个片段上,您返回视图,这样机器人果汁直到稍后才知道将其用于注入。
@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;
}https://stackoverflow.com/questions/23847333
复制相似问题