17

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

I am currently testing the new Maps API V2 but I'm really having trouble getting it to work correclty.

My problem is that getMap() always returns null.

I have tested the call in 3 different points:

  1. onCreate()
  2. onResume()
  3. in a Handler that is called some seconds after the map is already visible on the screen

Here is the code:

public class MapActivity extends FragmentActivity {

private SupportMapFragment mMapFragment;

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

@Override
protected void onResume() {
    super.onResume();
    setupMap();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            setupMap();
        }
    }, 5000);
}

private void setupMap() {
    if (getSupportFragmentManager().findFragmentById(R.id.fragment) == null) {
    mMapFragment = CustomMapFragment.newInstance();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.map_wrapper, mMapFragment).commit();
    }
    GoogleMap map = mMapFragment.getMap();
    if (map != null) {
        mMapFragment.getMap().getUiSettings().setZoomControlsEnabled(true);
        mMapFragment.getMap().getUiSettings().setZoomGesturesEnabled(true);
        mMapFragment.getMap().setMyLocationEnabled(true);
    }
}

Anything that I'm doing wrong?

Community
  • 1
  • 1
Goddchen
  • 4,459
  • 4
  • 33
  • 54
  • it indeed is, a shame that I didn't find that questions via the search function :( I will post my sample workaround as an answer. Thanks CommonsWare! – Goddchen Dec 28 '12 at 16:09
  • Firstly, what type of device were you running this code on? Physical device, or emulator (which may not have had Play Services installed). – IgorGanapolsky Mar 15 '16 at 17:41

1 Answers1

47

As CommonsWare stated in the linked question, the problem only occures when creating the SupportMapFragment programmatically and not a <fragment> XML tag.

If created programmatically, the map will be available in the onActivityCreated() call. So my workaround is the following:

mMapFragment = new SupportMapFragment() {
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                GoogleMap map = mMapFragment.getMap();
                if (map != null) {
                    //Your initialization code goes here
                }
            }
        };
Goddchen
  • 4,459
  • 4
  • 33
  • 54
  • 7
    One should not create non-static inner fragment because compiler will automatically create constructor to provide outer class to the fragment as an argument. Then on fragment recreation you will get `InstantiationException` because there is no default empty constructor for the fragment. – Alexander Mironov Jun 02 '13 at 06:50
  • How can you do this but also give options? Usually we instantiate with SupportMapFragment.newInstance(options); – Deminetix Nov 09 '13 at 07:21
  • you don't need options since you'll get variables as closure (if final), unfortunately the code is not usable see Alexander comment – sherpya Mar 13 '14 at 02:48
  • @AlexanderMironov I reckon that but i have made the `mMapFragment` static but still gets `InstantiationException`. Any help? – Muhammad Babar Oct 13 '14 at 10:53