Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, google maps fragment and viewpager - Error inflating class fragment

i'm trying to use google maps v2 within viewpager (fragmentpager) in a fragment. It works fine if i swipe to fourth fragment (view pager), but if i go back to the first fragment and then back to the fourth - with the map - my app dies.

07-04 19:38:20.937: E/AndroidRuntime(20175): FATAL EXCEPTION: main
07-04 19:38:20.937: E/AndroidRuntime(20175): android.view.InflateException: Binary XML     
file line #13: Error inflating class fragment
...
07-04 19:38:20.937: E/AndroidRuntime(20175): Caused by:     
java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f050010,
 tag null, or parent id 0x0 with another fragment for  com.google.android.gms.maps.SupportMapFragment

07-04 19:38:20.937: E/AndroidRuntime(20175):    at     android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-04 19:38:20.937: E/AndroidRuntime(20175):    at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

Android Java:

public class LocationDetail extends Fragment {

private SupportMapFragment mMapFragment;
private GoogleMap map;
private EventAdapter listAdapter ;

protected MainActivity activity;

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

    View vw = inflater.inflate(R.layout.map, container, false);

    try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO handle this situation
    }

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_container)).getMap();
    map.setMyLocationEnabled(true);


    map.addMarker(new MarkerOptions()
    .position(new LatLng(48.397141, 9.98787)).title(" Theatro Club Ulm"));

    map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(48.397141, 9.98787) , 14.0f) );

    return vw;
}

}

Layout:

<?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" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

 <fragment

    android:id="@+id/map_container"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.dr.diskoapp.MainActivity"
    />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

<ListView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list">          
</ListView>
</LinearLayout>
</LinearLayout>

I've seen that there are many threads with a similar problem, but no solution :-/

perhapse can anybody help this time.

---------- EDIT ----- I found the solution :-)

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

    try {
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_container));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
like image 336
Dominik00000 Avatar asked Jul 04 '13 18:07

Dominik00000


2 Answers

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

try {
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_container));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
} catch (Exception e) {
    e.printStackTrace();
}

}

like image 141
Roadies Avatar answered Nov 18 '22 06:11

Roadies


This worked for me.

@Override
public void onDestroyView() {
    // TODO Auto-generated method stub
    super.onDestroyView();
    try 
    {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.remove(mapFragment);
        ft.commitAllowingStateLoss();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
like image 1
Anirudh Avatar answered Nov 18 '22 06:11

Anirudh