Inflate fragment from activity

Viewed 11285

Here is my activity class. I am trying to inflate fragment to this activity.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fg1, new ButtonsFragment());
        ft.commit();
    }

activity_main.xml

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

    <fragment
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:id="@+id/fg1"
        />
</LinearLayout>

ButtonsFragment.java

public class ButtonsFragment extends Fragment {

    Button b1,b2,b3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        super.onCreateView(inflater,container,savedInstanceState);

       View v = inflater.inflate(R.layout.fragment_buttons,container,false);

        b2 = (Button)v.findViewById(R.id.b2);
        b1 = (Button)v.findViewById(R.id.b1);
        b3 = (Button)v.findViewById(R.id.b3);
        return v;
    }
}

fragment_buttons.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.iiitb.patientdoctormanagementsystem.ButtonsFragment">

 <LinearLayout
        android:id="@+id/ll"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:textAllCaps="false"
            android:text="Past Appointments"
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:textAllCaps="false"
            android:text="Todays Appointments"
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <Button
            android:textAllCaps="false"
            android:text="Upcoming Appointments"
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />
    </LinearLayout>
</FrameLayout>

But I am getting this exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iiitb.patientdoctormanagementsystem/com.example.iiitb.patientdoctormanagementsystem.MainActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class fragment 

PLEASE HELP!!

2 Answers

There are two ways you can add a fragment to the activity layout:

  • Declare the fragment inside the activity's layout file
  • Programmatically add the fragment to a ViewGroup

In your example you followed both approached at the same time. You should select one of them. When you added a fragment in activity layout:

<fragment
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1"
    />

You missed android:name attribute. This attribute specifies the Fragment class to instantiate in the layout. So, you should declare it like this:

 <fragment
    android:name="your.package.ButtonsFragment"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1"
    />

When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element. (from developer.android.com)

By following this approach you don't need these lines of code (remove it):

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fg1, new ButtonsFragment());
ft.commit();

If you want to follow the second approach, add a fragment to an existing ViewGroup programmatically, you should change activity layout as follow:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_margin="10dp"
    android:id="@+id/fg1" />

and leave other parts of your code as it is. For more information about fragments you can refer to android documentation

Try To Use FrameLayout Instead of fragment in activity_main.xml

// activity_main.xml

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:id="@+id/fg1"
        />
</LinearLayout>
Related