abidkhan303
abidkhan303

Reputation: 1791

Android Fragments Crash

I have just started learning Android Fragments. Please help my why my following program is crashing.

One Activity contains two fragments and each fragment has only one button to display. I tried to follow the example in Head First Android Development...BUT :(

Activity which contains fragments

public class FragmentsContainer extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragmentscontaineractivity);
    }
}

Layout of fragment container

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <fragment
            android:name="com.example.nasaimage.ImageOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <fragment
            android:name="com.example.nasaimage.ImageTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2" />
    </LinearLayout>

First Fragment and layout

 public class ImageOne extends Fragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater,
                ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.imageoneactivity, container, false);
        }
   }

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Second Activity and Layout

 public class ImageTwo extends Fragment {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

             View view =  inflater.inflate(R.layout.imagetwoactivity, container, false);

                Button button = (Button) view.findViewById(R.id.button1);
                button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {

                  }
                });
                return view;
        }
    }


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    </LinearLayout>

Upvotes: 1

Views: 2057

Answers (2)

Miral Sarwar
Miral Sarwar

Reputation: 327

Why are you using this approach, there's a smarter approach you can use... After pressing a button, or menu option or navigation drawer item the fragment can be invoked like this way:

FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();
            FragmentExcel fragmentS1 = new FragmentExcel();
            fragmentTransaction.replace(R.id.content_frame, fragmentS1);
            fragmentTransaction.commit();

Then the the fragment class can be like this where there are three buttons with specific functionalities:

public class FragmentExcel extends Fragment {    
    Button buttonCat, buttonBudg, buttonExpense;
    DBHelper db;

    public static Fragment newInstance(Context context) {
        FragmentHistory f = new FragmentHistory();    
        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {    
        if (container == null) {
            return null;
        }

        RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(
                R.layout.fragment_excel, container, false);

        db = new DBHelper((getActivity().getBaseContext()));

        buttonCat = (Button) mLinearLayout.findViewById(R.id.buttonCategoies);
        buttonBudg = (Button) mLinearLayout.findViewById(R.id.buttonBudgets);
        buttonExpense = (Button) mLinearLayout
                .findViewById(R.id.buttonExpenses);

        buttonCat.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                db = new DBHelper((getActivity().getBaseContext()));
                String tableName = "categories";
                db.exportDataIntoCSV(tableName);

                Toast.makeText(getActivity().getBaseContext(),
                        "File is saved in tour SDCard storage",
                        Toast.LENGTH_SHORT).show();    
            }
        });

        buttonBudg.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                //Do whatever you want    
            }
        });

        buttonExpense.setOnClickListener(new View.OnClickListener() {    
            @Override
            public void onClick(View v) {
                //Do whatever you want              }
        });

        return mLinearLayout;
    }
}

Upvotes: 1

dnkoutso
dnkoutso

Reputation: 6061

You should include the support library found here then make your Activity extend FragmentActivity.

Only Android 3.x and above added the FragmentManager and Fragment support into the base Activity class.

Upvotes: 0

Related Questions