2013-07-04 3 views
13

ich versuche google maps v2 innerhalb von viewpager (fragmentpager) in einem fragment zu verwenden. Es funktioniert gut, wenn ich zum vierten Fragment wische (Pager sehen), aber wenn ich zurück zum ersten Fragment gehe und dann zurück zum vierten - mit der Karte - stirbt meine App.Android, google maps fragment und viewpager - Fehler beim Aufblasen der Klassenfragmente

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> 

Ich habe gesehen, dass es viele Fäden mit einem ähnlichen Problem, aber keine Lösung: -/

Perhaps kann jemand diesmal helfen.

---------- EDIT ----- fand ich die Lösung :-)

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(); 
    } 
} 
+0

Betrachten Sie dies in eine Antwort (ja, beantworten Sie Ihre eigene Frage), damit andere sehen können, dass das Problem gelöst wurde! – DaGardner

+1

Sie haben Recht, so versuchte ich zuerst, aber ich habe nicht genug Ruf, um das zu tun :-( – Dominik00000

+0

Seltsam. Arbeitete gut für mich und mein Problem trat mit zwei Seiten auf - eine, die die Karte enthielt und eine, die didn Kann niemand erklären, was hier vor sich geht, um es zum Absturz zu bringen? –

Antwort

11
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(); 
} 

}

+0

Das hat mein Problem behoben. –

1

Das ist für mich gearbeitet.

@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(); 
    } 
} 
Verwandte Themen