使用roboguice不具体液活性

时间:2014-05-24 16:33:12

标签: android android-fragments guice roboguice

我有一种体育活动,我注入了一些像资源和观点这样的东西。我在我的应用程序中使用了一些片段类。这些片段必须扩展片段。这是我的东西:

按下按钮时,我会创建一个新片段:

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无法正常工作。我找不到它不能工作的原因。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

onViewCreated之后的onCreateView期间进行注射。将代码移至onViewCreated

ActivitysetContentView可以进行注射。在一个片段上,你返回视图,因此robojuice不知道在以后使用它进行注射。

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