3

我正在尝试实现地图,但遇到了不可转换类型的错误;不能投射android.support.v4.app.Fragmentcom.google.android.gms.maps.SupportMapFragment

我看过一些资源,但对我没有用

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

不可转换的类型;无法将“android.support.v4.app.Fragment”转换为“com.google.android.gms.maps.SupportMapFragment”

4

2 回答 2

4

根据Google Play 服务 Android 发行说明play-services-maps:17.0.0

警告:此版本是主要版本更新和重大更改。

  • 更新您的应用以使用 Jetpack (AndroidX);按照迁移到 AndroidX中的说明进行操作。

地图17.0.0已切换到AndroidX。这意味着SupportMapFragmentnow extends androidx.fragment.app.Fragment,而不是 Support Library 等价物。您需要切换回16.1.0或将您的应用迁移到 AndroidX。

于 2019-07-10T04:29:21.673 回答
0

代替:

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

和:

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
于 2019-09-30T11:14:32.093 回答