Android: fragments: onCreateView isn't called
I've got a sandbox project where everything works correctly, but not in real project. I guess I miss something...
In main activity I have (I've simplified a project as much as I could):
@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 Line开发者_开发百科arLayout.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;
}
And in SimpleFragment.java:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
TextView textView = new TextView(getActivity());
textView.setText("SimpleFragmentText");
return textView;
But when I start I see only 112233 without SimpleFragmentText. While debugging I've noticed that onCreateView method never being called... Why? It looks like the same code works great in standalone app... May be there are additional things I don't know about?
I've forgotten transaction.commit:
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ModelEditorFragment simpleFragment = new SimpleFragment();
transaction.add(11111, simpleFragment);
transaction.commit();
}
rootLayout.addView(textView);
{
rootLayout.addView(leftLayout);
{
FragmentTransaction transaction = getFragmentManager().beginTransaction();
ModelEditorFragment simpleFragment = new SimpleFragment();
transaction.add(11111, simpleFragment);
}
}
return rootLayout;
Try that I think your rootLayout is just missing the textView addition part
精彩评论