2013-06-06 11 views
9

Ich habe Probleme, auf das Fragment der Map zuzugreifen. getFragmentManager() .findFragmentById (R.id.map)) gibt immer null zurück. Ich weiß nicht warum. Was ist das Problem?Mapfragment findFragmentById immer null

Vielen Dank!

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

     <TextView 
      android:id="@+id/tvNombreCentro" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingBottom="10dp" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 


    <fragment 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="tag_fragment_map" /> 

    </LinearLayout> 

Und in der Tätigkeit nach dem setContentView ich versuche, auf die Karte zugreifen zu können, aber ich erhalte eine Ausnahme

public class Mapa extends Activity { 
private GoogleMap mMap; 
private ActionBar ab; 

private TextView tvNombreCentro; 
private TextView tvTelefonoValor; 
private TextView tvEMailValor; 
private TextView tvWebValor; 
private TextView tvDireccionValor; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.mapa_centro); 
    GoogleMap mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
      .getMap(); 
} 

Antwort

21

Verwendung auf diese Weise:

SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager() 
        .findFragmentById(R.id.map); 

      map = mapFrag.getMap(); 

here is full code with sample:

+0

Entschuldigung für die Verzögerung. Es klappt! – user1852854

+11

Wenn Sie die Android-Unterstützungsbibliothek zum Anzeigen einer Karte verwenden, stellen Sie bitte sicher, dass Ihre Layout-XML-Datei, die das Kartenfragment enthält, den Wert "android: name" für "com.google.android.gms.maps.SupportMapFragment" hat 'anstatt' com.google.android.gms.maps.MapFragment', sonst wird es nicht funktionieren und 'null' – Aryo

+0

Excellent! es hilft mir!. –

2

Wenn Ihr Problem nicht gelöst wird, versuchen Sie dies:

Wie Sie verwenden SupportMapFragment, während Karte abrufen, verwenden

GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 

auch chan ge

public class Mapa extends Activity { 

zu

public class Mapa extends FragmentActivity { 
+0

Entschuldigung für die Verzögerung. Die Antwort ist korrekt, aber ich brauche die extends nicht zu ändern, nur getSupportFragmentManager. Vielen Dank! – user1852854

1

Wechsel im Layout

android:name = "com.google.android.gms.maps.SupportMapFragment"

von

android:name = "com.google.android.gms.maps.MapFragment".

Oder Sie können getSupportFragmentManager() anstelle von getFragmentManager() verwenden.

1

Ich hatte ein ähnliches Problem beim Versuch MapFragment zu erhalten, aber null zu erhalten, da auf dem Gerät die Google Play Services nicht aktualisiert wurden.