1

I can build an instance of Google Map and view the map but I can't getMap(), it's always null:

mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                .beginTransaction();
        fragmentTransaction.replace(R.id.container, mMapFragment);
        fragmentTransaction.commit();
        if (mMap == null) {
            mMap = mMapFragment.getMap();
                    // always null, why?
            if (mMap != null) {
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
            }
        }
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
  • replacing Fragment is asynchronous, so this fragment could event not be already placed on your layout, so making getMap gives you null. You should replace/add your mapFragment and request for map with getMap method after this mapFragment transaction ended – michal.luszczuk Apr 19 '14 at 20:20
  • See http://stackoverflow.com/a/14909970/1051804 answer – michal.luszczuk Apr 19 '14 at 20:24

2 Answers2

0
This works pretty well for me ...
It seems like it's the default background (which is black) of SurfaceView which is causing this bug .
public class MyMapFragment extends SupportMapFragment() {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View view = super.onCreateView(inflater, container, savedInstanceState);
  setMapTransparent((ViewGroup) view);
  return view;
 };

 private void setMapTransparent(ViewGroup group) {
  int childCount = group.getChildCount();
  for (int i = 0; i < childCount; i++) {
   View child = group.getChildAt(i);
   if (child instanceof ViewGroup) {
    setMapTransparent((ViewGroup) child);
   } else if (child instanceof SurfaceView) {
    child.setBackgroundColor(0x00000000);
   }
  }
 }

};
M.Ganji
  • 818
  • 8
  • 13
0

Map will be available in the onActivityCreated() when created programmatically. so, try like this:

mMapFragment = new SupportMapFragment() {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

           if (mMap == null) {
                mMap = this.getMap();

                //other code
        }
    };

Check here: How do I know the map is ready to get used when using the SupportMapFragment?

Community
  • 1
  • 1
Srikanth
  • 1,555
  • 12
  • 20