不可转换类型;无法将 'androidx.fragment.app.Fragment' 转换为 'com.google.android.gms.maps.MapFragment'

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

在我的片段中:

 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)

此处布局:

<?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>

但是我得到编译错误:

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

MapFragment 现在仅在您的应用面向 api 12 及更高版本时使用。 https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment。您可以考虑改用 SupportMapFragment。

在你的布局文件中

<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" />

在你的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);
}

您可以在开发者网站上获取更多信息:https://developers.google.com/maps/documentation/android-sdk/map-with-marker