0

I'm developing an app which uses fragment tab, one of my fragment uses Google Maps V2 "SupportMapFragment"

public class A extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
   {
    View view = null;
    view = inflater.inflate(R.layout.all_coffee_shops_view, container, false);
    map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
   }
}

my xml:

<RelativeLayout
   android:id="@+id/map_container"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <fragment
     android:id="@+id/map"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     class="com.google.android.gms.maps.SupportMapFragment" 
   />
</RelativeLayout>

I'm calling B Fragment from this A fragment(A->B) and when I come back from B->, A fragment onCreateView throws an exception, "android.view.InflateException: Binary XML file line #132: Error inflating class fragment"

maximus ツ
  • 7,949
  • 3
  • 25
  • 54
  • Check my answer on SO at this link : http://stackoverflow.com/questions/19353255/how-to-put-google-maps-v2-on-a-fragment-using-viewpager/19354359#19354359 – arshu Oct 15 '13 at 09:16

2 Answers2

6

when once map fragment is created and when comes back to this fragment again fragment is onCreateView() is called that's why you are getting force close

Try this once in your fragment containing map

@Override
public void onDestroyView() {
    super.onDestroyView();

    Fragment f = getFragmentManager().findFragmentById(R.id.map);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}

please comment me about the result

NARESH REDDY
  • 682
  • 4
  • 11
  • Hey NARESH REDDY, Thanks for answering me question... since i'm using fragment tabs i'm using a base container for handle the view hierarchy in tabs, you code didn't work, but i used getSupportFragmentManager for getFragmentManager, then it worked like a charm... – Kasun Wanniarachchi Oct 15 '13 at 09:20
2
public void onDestroyView() {
    super.onDestroyView();
    Log.d(TAG, "onDestroyView");

    Fragment f = getActivity()
            .getSupportFragmentManager().findFragmentById(R.id.map);
    if (f != null) {
        getActivity().getSupportFragmentManager()
        .beginTransaction().remove(f).commit();
    }
}