In this example I can navigate between 3 pages by swipe. The first page (page0.xml) contains a fragment (@+id/fragment1). Works fine when swiping forward, but the problem is when navigating back from page2 to page1, the application crash.
Here is stacktrace
FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #14 Error inflating class fragment
How to solve this?
package crash;
import com.example.crash.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;  
  public class Main extends FragmentActivity{
@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    setContentView(R.layout.main);
    FragmentManager fm = getSupportFragmentManager();
    ViewPager vp = (ViewPager) findViewById(R.id.my_viewpager);
    MyAdapter ad = new MyAdapter(fm);
    vp.setAdapter(ad);
    super.onCreate(arg0);
}
public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
        // this.myContext=myContext;
    }
    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Bundle bu = new Bundle();
        bu.putInt("page", arg0);
        Fragment myFragment = new MyFragment();
        myFragment.setArguments(bu);
        return myFragment;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }
}
public static class MyFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v;
        Bundle bu = getArguments();
        switch (bu.getInt("page")) {
        case 0:
            return inflater.inflate(R.layout.page0, container, false);
        case 1:
            return inflater.inflate(R.layout.page1, container, false);
        case 2:
            return inflater.inflate(R.layout.page2, container, false);
        default:
            return null;
        }
    }
    public static class MyIncludedFragment extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.fragment,container,false);
        }
    }
}
}
main.xml
<?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="vertical" >
 <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:id="@+id/my_viewpager"
        >
 </android.support.v4.view.ViewPager>   
</LinearLayout>
page0.xml
   <?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="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Page 0"
    android:textSize="48dp" />
<fragment
    android:id="@+id/fragment1"
    android:name="crash.Main$MyFragment$MyIncludedFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
page1.xml
<?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="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Page 1"
        android:textSize="48dp" />
</LinearLayout>
page2.xml
<?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="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Page 2"
        android:textSize="48dp" />
</LinearLayout>
fragment.xml
<?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:background="#0ff"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My fragment"
        android:textSize="48dp" />
</LinearLayout>
 
     
     
    