Android: фрагменты: onCreateView не вызывается - PullRequest
3 голосов
/ 02 августа 2011

У меня есть проект песочницы, где все работает правильно, но не в реальном проекте. Наверное, я что-то упустил ...

В основной деятельности, которую я имею (я максимально упростил проект):

@Override
  public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(createUI());
  }

  public View createUI() {
    LinearLayout rootLayout = new LinearLayout(this);
    rootLayout.setOrientation(LinearLayout.HORIZONTAL);
    rootLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    LinearLayout leftLayout = new LinearLayout(this);
    leftLayout.setLayoutParams(new LinearLayout.LayoutParams(300, ViewGroup.LayoutParams.FILL_PARENT));
    leftLayout.setId(11111);

    android.widget.TextView textView = new android.widget.TextView(this);
    textView.setText("112233");

    rootLayout.addView(textView);
    rootLayout.addView(leftLayout);
    {
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      ModelEditorFragment simpleFragment = new SimpleFragment();
      transaction.add(11111, simpleFragment);
    }
    return rootLayout;
  }

А в SimpleFragment.java:

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    TextView textView = new TextView(getActivity());
    textView.setText("SimpleFragmentText");
    return textView;

Но при запуске я вижу только 112233 без SimpleFragmentText. Во время отладки я заметил, что метод onCreateView никогда не вызывается ... Почему? Похоже, что тот же код отлично работает в автономном приложении ... Может быть, есть дополнительные вещи, о которых я не знаю?

Ответы [ 2 ]

2 голосов
/ 03 августа 2011

Я забыл транзакции.commit:

{
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      ModelEditorFragment simpleFragment = new SimpleFragment();
      transaction.add(11111, simpleFragment);
      transaction.commit();
    }
0 голосов
/ 02 августа 2011
rootLayout.addView(textView);
{
    rootLayout.addView(leftLayout);
    {
      FragmentTransaction transaction = getFragmentManager().beginTransaction();
      ModelEditorFragment simpleFragment = new SimpleFragment();
      transaction.add(11111, simpleFragment);
    }
}
    return rootLayout;

Попробуйте, я думаю, в вашем rootLayout просто отсутствует часть добавления textView

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...