Mido Ashraf
Mido Ashraf

Reputation: 109

Inconvertible types Cannot cast

I am trying to implement maps and I am getting error of Inconvertible types; cannot cast android.support.v4.app.Fragment to com.google.android.gms.maps.SupportMapFragment

I have seen some resources but nothing works for me

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;

import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements         OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'

Upvotes: 4

Views: 4260

Answers (2)

Thamsanqa Shabalala
Thamsanqa Shabalala

Reputation: 21

Replace:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

with:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 199805

As per the Google Play services Android release notes for play-services-maps:17.0.0:

Warning: This release is a MAJOR version update and breaking change.

Maps 17.0.0 has switched to AndroidX. That means that SupportMapFragment now extends androidx.fragment.app.Fragment, not the Support Library equivalent. You need to either switch back to 16.1.0 or migrate your app to AndroidX.

Upvotes: 4

Related Questions