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

时间:2019-07-06 15:24:38

标签: android android-maps-v2

在我的片段中:

=SUMPRODUCT(--((D:D)="Pablo Neruda Cabernet Savignon"))

此处布局:

 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>

1 个答案:

答案 0 :(得分:1)

现在仅在您的应用定位到api 12及更高版本时才使用MapFragment。 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" />

您的活动

@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