【问题标题】:Use roboguice out of roboactivity [duplicate]在机器人活动中使用 roboguice [重复]
【发布时间】:2014-05-24 16:33:12
【问题描述】:

我有一个机器人活动,我在其中注入一些东西,比如资源和视图。我在我的应用程序中使用了一些片段类。这些片段必须扩展片段。这是我的东西:

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

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 不工作。我找不到它不起作用的原因。谁能帮我解决这个问题?

【问题讨论】:

    标签: android android-fragments guice roboguice


    【解决方案1】:

    注入发生在onViewCreated 期间,在onCreateView 之后。将您的代码移至onViewCreated

    Activity 上有setContentView 可以进行注入。在片段上,您返回视图,因此 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2015-05-18
      相关资源
      最近更新 更多