Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

android - MapFragment return null

mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mMapFragment == null) {
          mMapFragment = SupportMapFragment.newInstance();


            // Then we add it using a FragmentTransaction.
            FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(MapLay.getId(), mMapFragment, MAP_FRAGMENT_TAG);
             fragmentTransaction.commit();


            mMap=mMapFragment.getMap();

By this code map is visible but unable to access the map

mMap=mMapFragment.getMap(); show null value error how to fix this

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update 1: getMap() is deprecated

It is better to use getMapAsync() method of MapFragment/SupportMapFragment. The example how to use the method shown below (copied from their documentation).

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }

}

Quoting Google's MapFragment/SupportMapFragment

A GoogleMap can only be acquired using getMap() when the underlying maps system is loaded and the underlying view in the fragment exists. This class automatically initializes the maps system and the view; however you cannot be guaranteed when it will be ready because this depends on the availability of the Google Play services APK. If a GoogleMap is not available, getMap() will return null.

On your code, you immediately retrieve GoogleMap AFTER Committing MapFragment. Wait until the MapFragment is fully loaded on activity so you can get the GoogleMap.

Perhaps, you can deliver the GoogleMap from MapFragment to Activity using interface, like this.

public class MyMapFragment extends SupportMapFragment
{
  private MapCallback callback;

  public void setMapCallback(MapCallback callback)
  {
    this.callback = callback;
  }

  public static interface MapCallback
  {
     public void onMapReady(GoogleMap map);
  }

  @Override public void onActivityCreated(Bundle savedInstanceState)
  {
     super.onActivityCreated(savedInstanceState);
     if(callback != null) callback.onMapReady(getMap());     
  }
}


public class MyActivity extends Activity implements MyMapFragment.MapCallback
{
   // .........
  @Override
  public void onCreate(Bundle onsavedInstanceState)
  {
        mMapFragment = (MyMapFragment) getSupportFragmentManager()
                .findFragmentByTag(MAP_FRAGMENT_TAG);

        // We only create a fragment if it doesn't already exist.
        if (mMapFragment == null) {
               mMapFragment = MyMapFragment.newInstance();

               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded

               // Then we add it using a FragmentTransaction.
               FragmentTransaction fragmentTransaction =
                    getSupportFragmentManager().beginTransaction();
               fragmentTransaction.add(MapLay.getId(), mMapFragment, MAP_FRAGMENT_TAG);
               fragmentTransaction.commit();

         }
         else
         {
               mMapFragment.setMapCallback(this); // This activity will receive the Map object once the map fragment is fully loaded
         }

  @Override
  public void onMapReady(GoogleMap map)
  {
     // Do what you want to map
  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...