0

Please help! I have three fragments that need to be shown on the gallery page, but my app keeps crashing. This is the gallery.java page

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
 import android.os.Bundle;


public class Gallery extends Activity {
Fragment fragmenta;
Fragment fragmentb;
Fragment fragmentc;
FragmentManager fm;
String fraga;
String fragb;
String fragc;
FragmentTransaction ft;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);
    fm = getFragmentManager();
    fragmenta=new FragmentA();
    fragmentb=new FragmentB();
    fragmentc=new FragmentC();
    fragmenta=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmenta);
    fragmentb=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmentb);
    fragmentc=(Fragment)getFragmentManager().findFragmentById(R.layout.fragmentc);

  //  getFragmentManager().beginTransaction().add(R.id.fragment,fragmenta,fraga).commit();
  //  getFragmentManager().beginTransaction().add(R.id.fragment2,fragmentb,fragb).commit();
  //  getFragmentManager().beginTransaction().add(R.id.fragment3,fragmentc,fragc).commit();

    ft=fm.beginTransaction();
    ft.add(R.id.fragment, new FragmentA());
    ft.add(R.id.fragment2, new FragmentB());
    ft.add(R.id.fragment3, new FragmentC());
    ft.commit();





}

}

this is frag a. java

public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragmenta,container,false);
    return v;
}

This is frag b.java

public class FragmentB extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.fragmentb,container,false);
    return v;
}

this is frag c.java

public class FragmentC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v= inflater.inflate(R.layout.fragmentc,container,false);
    return v;
}

please help

there are three fragments and i need three of them to show on the gallery page. the xml code is ok, so i didn't think to add it here

my gallery.xml

<?xml version="1.0" encoding="utf-8"?>

<fragment
    android:layout_width="350dp"
    android:layout_height="70dp"
    android:name="android.app.DialogFragment"
    android:id="@+id/fragment"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp" />

<fragment
    android:layout_width="320dp"
    android:layout_height="250dp"
    android:name="android.app.DialogFragment"
    android:id="@+id/fragment2"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<fragment
    android:layout_width="350dp"
    android:layout_height="70dp"
    android:name="android.app.DialogFragment"
    android:id="@+id/fragment3"
    android:layout_below="@+id/fragment2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp" />

Aria
  • 389
  • 3
  • 7
  • 25

1 Answers1

0

Try this

<?xml version="1.0" encoding="utf-8"?>
<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="com.example.yourApp.FragmentA"
android:id="@+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />

<fragment
android:layout_width="320dp"
android:layout_height="250dp"
android:name="com.example.yourApp.FragmentB"
android:id="@+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<fragment
android:layout_width="350dp"
android:layout_height="70dp"
android:name="com.example.yourApp.FragmentC"
android:id="@+id/fragment3"
android:layout_below="@+id/fragment2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
Want2bExpert
  • 527
  • 4
  • 11