EyeQ Tech
EyeQ Tech

Reputation: 7358

Android SupportMapFragment.getMap() returns null

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

Upvotes: 2

Views: 5297

Answers (2)

technocrat
technocrat

Reputation: 3705

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"

Upvotes: 3

marshallino16
marshallino16

Reputation: 2671

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

Upvotes: 2

Related Questions