Alexei
Alexei

Reputation: 15656

Inconvertible types; cannot cast 'androidx.fragment.app.Fragment' to 'com.google.android.gms.maps.MapFragment'

In my fragment:

 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;

public class AgentsFragmentMapTab extends Fragment {

 com.google.android.gms.maps.MapFragment mapFragment = (com.google.android.gms.maps.MapFragment) getFragmentManager().findFragmentById(R.id.google_map)

here layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <fragment
            android:id="@+id/google_map"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>

but I get compile error:

Inconvertible types; cannot cast 'androidx.fragment.app.Fragment' to 'com.google.android.gms.maps.MapFragment'

Upvotes: 3

Views: 8145

Answers (1)

Gelo
Gelo

Reputation: 320

MapFragment is now only used if your app is targeting api 12 and above. https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment. You might consider using SupportMapFragment instead.

In your layout file

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />

In your activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Retrieve the content view that renders the map.
    setContentView(R.layout.activity_maps);
    // Get the SupportMapFragment and request notification
    // when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
        .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

You can get more info on the developer site: https://developers.google.com/maps/documentation/android-sdk/map-with-marker

Upvotes: 2

Related Questions