Comment créer de multiples fragments par programmation?

Je suis en train de montrer de multiples fragments sur un écran par la création par programmation. Je suis capable de faire ce pas de problème en incluant chaque fragment dans les activités de mise en page du fichier. Cependant quand j'ai essayer de le faire par programmation Im confus.C'est ce que j'ai jusqu'à présent pour les deux fragments sur un écran.

Classe Principale:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.imOne, one, "fragmentone");
        ft.add(R.id.imTwo, two, "fragmenttwo");
        ft.add(R.id.imThree, three, "fragmenthree");
        ft.commit();

    }
}

Fragment:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_one, container, false);
        return view;
    }

}

Fragment De Deux:

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_two, container, false);
        return view;
    }

}

Les fichiers de mise en page pour mes deux fragment de classes contiennent un simple TextView.
Et puis l'activity_main.xml fichier de mise en page pour ma classe principale:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/imOne"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imTwo"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imThree"
        android:name="com.david.twofragexample.FragmentThree"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

Quand j'ai essayer de le faire par programmation-je obtenir confus quant à ce qui devrait être dans mon activity_main.xml fichier. Donc, à partir de l'Android Developer Guide http://developer.android.com/guide/components/fragments.html) il est dit d'utiliser

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Alors quand j'ai ajouter ce code à ma méthode onCreate quelles modifications dois-je apporter à ma activity_main.xml fichier de le faire par programmation?Je ne vois pas où R. id.fragment_container vient de. Comment puis-je déterminer où sur l'écran de mon nouveau fragment va?Grâce

EDIT: je suis en train d'élaborer ce sur une tablette et non d'un téléphone.Cela a maintenant été résolu et le code ci-dessus va ajouter trois fragments de la programmation.

Avez-vous des fragment_container élément dans votre activity_main.xml?
Oui juste ajouté ça là merci

OriginalL'auteur DMC | 2013-01-18