【问题标题】:Android & RoboGuice - Inject views on Fragment?Android 和 RoboGuice - 在 Fragment 上注入视图?
【发布时间】:2012-04-04 13:32:10
【问题描述】:

我有一个片段需要在屏幕上显示。我希望能够使用InjectView 来注入我的 UI 元素。 InjectView 在活动上运行良好,因为视图 ​​(xml) 是在 onCreate 期间设置的,但是在片段上,视图是在 onCreatView 上设置的。

那么有没有办法在片段上使用 InjectView 呢?我知道我可以使用 findViewbyId 来查找每个元素,但我更愿意使用 InjectView

public class ProfileFragment extends RoboDialogFragment {

    @InjectView(R.id.commentEditText)
    protected EditText commentEditText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            // I get a  null pointer exception here
            commentEditText.setText("Some comment");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.profile , container, false);

            // I get a  null pointer exception here
        commentEditText.setText("Some comment");

        return view;
    }

}

【问题讨论】:

标签: android fragment inject roboguice


【解决方案1】:

注入发生在onViewCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    commentEditText.setText("Some comment");
}

【讨论】:

  • 我们将努力为 2.0 提供更好的文档,因为它已在候选版本中
  • @emmby 文档顺利吗?它已经发布了一分钟,但仍然缺乏文档,至少对于 Fragments 的一个好的和彻底的例子来说。
  • 我们现在正在处理文档。他们正在变得更好,但还没有:github.com/emmby/roboguice/wiki
  • @emmby 已经 2 年半了,这个在 roboguice 中非常基本和常见的成语仍然没有记录!
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多