Ich versuche, eine Karte von einem SupportMapFragment zu erhalten, aber es gibt null zurück. Von was ich gelesen habe, könnte das sein, weil das Fragment noch nicht vollständig angezeigt wird und daher keine Karte existiert ?! Ich habe versucht, es mit executePendingTransactions() zu beheben, aber bisher ohne Erfolg.mapFragment.getMap() gibt null zurück
Irgendwelche Ideen, wie man es repariert? Hier
ist der Code
private GoogleMap map;
private SupportMapFragment mapFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
//...
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_mission2);
GoogleMapOptions mapOptions = new GoogleMapOptions();
mapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(true)
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentManager.enableDebugLogging(true);
mapFragment = SupportMapFragment.newInstance(mapOptions);
FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.mapFragment, mapFragment);
fragmentTransaction.commit();
myFragmentManager.executePendingTransactions();
if(mapFragment == null) Base.log("mapFragment==null");
if(map==null){
map = mapFragment.getMap();
Base.log("map should have been initialized");
if(map==null) Base.log("map still null");
}
}
Und die Layout-Datei:
<fragment
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Es gibt folgende Protokoll
V/FragmentManager(24224): add: SupportMapFragment{4078c4b8 id=0x7f06003d}
V/FragmentManager(24224): Allocated fragment index SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
V/FragmentManager(24224): moveto CREATED: SupportMapFragment{4078c4b8 #1 id=0x7f06003d}
D/EMR (24224): map should have been initialized
D/EMR (24224): map still null
angegeben haben hier erklärt Sie die 'mapFragment' Variable? – yugidroid
Ja das habe ich deklariert (bearbeitet den Codeteil, siehe oben) – cos4
Ist es initialisiert? Sonst ist es dasselbe wie nichts, d. H. "Null". – yugidroid