我有一個沙盒項目,一切正常,但不是在真正的項目。 我想我錯過了什麼......Android:片段:onCreateView不叫
在主要活動中我有(我已經簡化,就像我能項目):
@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方法永遠不會被調用......爲什麼?它看起來像相同的代碼在獨立的應用程序很好...可能是有我不知道的其他東西?
遺憾。我剛剛在實際項目中忘記了transaction.commit()... – leshka