MapFragment return null

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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

回答1:

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   }  }


回答2:

import part

import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment;

inside the oncreate

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

And Xml Part

Make sure you have put in

inside your tags.

and give these permission in manifet file



回答3:

Your xml should have SupportMapFragment



回答4:

I solved this problem when I put this code:

@Override public void onPause() {      Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_tab));       FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();     ft.remove(fragment);     ft.commit();     super.onPause(); }

or I put this code in onCreate

if (mView != null) {      ViewGroup parent = (ViewGroup) mView.getParent();      if (parent != null) {          parent.removeView(mView);      }  }  try {   mView = inflater.inflate(R.layout.tab_map_layout, container, false);  } catch (InflateException e) {   }


回答5:

Following the @Glenn-- comment from Oct 1 '13 at 6:05 to create this piece of source code.

My Implementation is replacing the SupportMapFragment with MapFragment and support Google Maps Version 2

I want to remember again:

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.

My Implementation:

AndroidManifest.xml

   

activity_main.xml

ApplicationMapFragment.java

public class ApplicationMapFragment extends MapFragment {      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());      }      /**      * Initialize default Google Maps Options for our Application      * @return GoogleMapOptions      */     public GoogleMapOptions initializeGoogleMapsOptions() {         GoogleMapOptions googleMapOptions = new GoogleMapOptions()             .mapType(GoogleMap.MAP_TYPE_HYBRID);          return googleMapOptions;     } }

MainActivity.java

public class MainActivity extends Activity implements ApplicationMapFragment.MapCallback {      // Get Class Name     private static String TAG = MainActivity.class.getName();      // Create a new Map fragment     private ApplicationMapFragment mapFragment;      // Google Map Fragment Name     private static String MAP_FRAGMENT_TAG = "google_maps_fragment";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          try {                 initilizeMapFragment();         } catch (Exception e) {                 e.printStackTrace();                 Log.e(TAG, "Google Maps can't be loaded", e);         }     }      /**      * Initialize a new Map Fragment      */     private void initilizeMapFragment() {          // Try to get Map Fragment         mapFragment = (ApplicationMapFragment) getFragmentManager()                 .findFragmentByTag(MAP_FRAGMENT_TAG);          // We only create a fragment if it doesn't already exist.         if (mapFragment == null) {             mapFragment = new ApplicationMapFragment();             mapFragment.initializeGoogleMapsOptions();              // This activity will receive the Map object once the map fragment is fully loaded             mapFragment.setMapCallback(this);              // Then we add it using a FragmentTransaction.             FragmentTransaction fragmentTransaction =                     getFragmentManager().beginTransaction();             fragmentTransaction.add(R.id.google_map_container, mapFragment, MAP_FRAGMENT_TAG);             fragmentTransaction.commit();         } else {             // This activity will receive the Map object once the map fragment is fully loaded             mapFragment.setMapCallback(this);         }     }      @Override     public void onMapReady(GoogleMap map) {         Log.d(TAG, "Google Map is loaded");          MarkerOptions marker = new MarkerOptions()             .position(new LatLng(10, 10))             .title("Hello World");          map.addMarker(marker);     } }


回答6:

Really hackish solution but, because given the poor fragment API...

Note that I'm doing this from a custom View and accessing the Activity using (Activity)getContext()

            addOnLayoutChangeListener(new OnLayoutChangeListener(){                 @Override                 public void onLayoutChange(View v, int left, int top,                         int right, int bottom, int oldLeft, int oldTop,                         int oldRight, int oldBottom) {                      GoogleMap map = mapFragment.getMap();                      if (map != null) {                          //Do stuff                     }else{                          removeOnLayoutChangeListener(this);                     }                 }             });


转载请标明出处:MapFragment return null
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!