SupportMapFragment.getmap() returns null
Asked Answered
E

6

5

I'm trying to load SupportMapFragment dynamically in a fragment, here is my onCreateView() method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View contentView = inflater.inflate(R.layout.frag_map_layout, null);
            shopDtos=ShopListFragment.shopDtos;
            fragment =SupportMapFragment.newInstance();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.map_content, fragment);
            ft.commit();
            map = fragment.getMap();
            mapMarker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0))
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.maps_pin)));
        return contentView;
    }

Unfortunately, I get the GoogleMap map as null. Any suggestions on to how create a mapfragment dynamically?

Erleena answered 7/10, 2013 at 7:33 Comment(3)
Check logs, most likely you have not enabled google maps API in play services console.Rights
no.. its loading when we add frgment directly to the xml file... but.... the problem is that... frgment.getmap() returns null.. so that i cant add markers to the mapErleena
possible duplicate of Initialize MapFragment programmatically with Maps API v2Chino
F
13

map takes some time to load, so you need to run your code in handler -->

Handler handler = new Handler();
handler.postDelayed(new Runnable() 

   @Override
   public void run() {
       GoogleMap googleMap = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true)).getMap();
       FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.map_content, fragment);
        ft.commit();
       if(googleMap != null) {
          googleMap.addMarker(new MarkerOptions().position(result)).setVisible(true);

          // Move the camera instantly to location with a zoom of 15.
          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(result, 15));

          // Zoom in, animating the camera.
          googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

          googleMap.getUiSettings().setZoomControlsEnabled(false);
          googleMap.getUiSettings().setCompassEnabled(false);
          googleMap.getUiSettings().setMyLocationButtonEnabled(false);

          handler.removeCallbacksAndMessages(null);
       }

       else {
            handler.postDelayed(this, 500);
       }
 }, 500);
Franklyn answered 7/10, 2013 at 7:52 Comment(4)
the map is loading but the problem is same can't add a marker to the mapErleena
Why is a Handler necessary here? Why not do this code in onActivityCreated()?Tutelary
Here is a link to a more robust solution: https://mcmap.net/q/1591557/-mapfragment-return-nullLimestone
@Limestone thanks for sharing the solution, I think your solution is simple and clean.Franklyn
H
3

Just try using the following code. It worked for me.

public class MapFragment extends SupportMapFragment {

    GoogleMap mapView;
    private Context context;

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

    @Override
    public View onCreateView(LayoutInflater mInflater, ViewGroup arg1,
        Bundle arg2) {
        View view = super.onCreateView(mInflater, arg1, arg2);

        return view;
    }

    @Override
    public void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2) {
        super.onInflate(arg0, arg1, arg2);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        mapView = getMap();
    }
}
Housecarl answered 24/1, 2014 at 4:49 Comment(0)
Z
2

There's a bunch of duplicate questions that go around the same topic... I can't seem to figure out how to flag it as a duplicate and then put one answer on it...

Here's the link to the other one: Android SupportMapFragment.getMap() returns null

And the potential answer posted there as well:

Check your layout xml file. I noticed that I wasn't referencing: android:name="com.google.android.gms.maps.SupportMapFragment" but rather: android:name="com.google.android.gms.maps.MapFragment"

The name should be: android:name="com.google.android.gms.maps.SupportMapFragment"

Zilpah answered 5/12, 2014 at 16:57 Comment(0)
B
1

It happened with me once in a new purchased device with Google play services not yet installed /updated. With other notes mentioned of course also consider this one.

Busman answered 15/12, 2014 at 19:53 Comment(0)
P
1

An up to date solution for this problem is to use getMapAsync (OnMapReadyCallback callback) instead.

Pungent answered 7/12, 2015 at 12:51 Comment(0)
C
0
getChildFragmentManager().findFragmentById(R.id.map)

Try getChildFragmentManager() in Fragment.

Connivance answered 30/9, 2016 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.