2

in my following code, getMap() returns null, which stop the app. If I don't do anything with the map (i.e, removing the last two lines), it shows correctly. Any idea why? Thanks.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        mapFragmentId = mapFragment.getId();
        FragmentTransaction transaction =  fragmentManager.beginTransaction();
        transaction.add(R.id.my_container, mapFragment);
        transaction.commit();

        mMap = mapFragment.getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • 1
    duplicate of [How do I know the map is ready to get used when using the SupportMapFragment?](http://stackoverflow.com/questions/14047257/how-do-i-know-the-map-is-ready-to-get-used-when-using-the-supportmapfragment) and http://stackoverflow.com/questions/14067889/google-play-service-supportmapfragment-getmap-always-returning-null and http://stackoverflow.com/questions/16322017/supportmapfragments-getmap-returns-null and others. – CommonsWare Sep 03 '13 at 14:21
  • tks @CommonsWare for good pointers. – EyeQ Tech Sep 03 '13 at 14:46
  • @CommonsWare just for discussion, if I inject the code `mMap = mapFragment.getMap()` in onResume() so that I don't have to override onActivityCreated, is it a bad practice? tks – EyeQ Tech Sep 03 '13 at 14:48
  • yes it is and will not avoid the crash of the app – marshallino16 Sep 03 '13 at 15:00
  • "is it a bad practice?" -- if it works, it is probably not a bad practice. I do not know if it works, though. – CommonsWare Sep 03 '13 at 15:05
  • Putting that in `onResume` is ok if you guard your initialization code to be called only one per `Activity` instance. If it's only setting map type, that's fine, but if you add visual objects there, that can be hard to find bug. – MaciejGórski Sep 03 '13 at 15:22

2 Answers2

3

Duplicate of: SupportMapFragment.getmap() returns null

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"

Community
  • 1
  • 1
technocrat
  • 3,513
  • 5
  • 25
  • 39
2

If getMap() is returning null it's because Map isn't ready yet or GooglePlayServices is out to date. You better use something like that :

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(resultCode != ConnectionResult.SUCCESS)
        {
            Builder builder = new AlertDialog.Builder(Main.this);
            builder.setMessage(getResources().getString(R.string.error_getting_maps));
            builder.setCancelable(true);
            builder.setPositiveButton("OK", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        } else {
            _map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            [ETC]
        }
marshallino16
  • 2,645
  • 15
  • 29
  • Thanks this was very helpful. You'll notice something like the following in the logs when the version of play services is out of date or missing. **6520-6520/com.something.myapp W/GooglePlayServicesUtil﹕ Google Play services out of date. Requires 6171000 but found 5089030** – Aaron Dancygier Jan 17 '15 at 08:48