错误:类型不兼容:片段无法转换为SupportMapFragment

时间:2019-06-27 04:21:47

标签: java android google-maps android-fragments

enter image description here我想为我的项目添加Google地图。我正在尝试这样做,那么我有这个问题

public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_map3);
        // 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);
  

xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".DriverMapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

3 个答案:

答案 0 :(得分:1)

 1. User MapView in place of Fragment

     <com.google.android.gms.maps.MapView
                    android:id="@+id/provider_map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />


 2. Update your gradle files as follows
    apply plugin: 'com.google.gms.google-services'    
    implementation 'com.google.android.gms:play-services-maps:17.0.0'    
    implementation 'com.google.android.gms:play-services-gcm:17.0.0'
    implementation 'com.google.android.gms:play-services:12.0.1'    
    classpath 'com.google.gms:google-services:4.3.2' 

答案 1 :(得分:0)

尝试

步骤1-在您的xml文件中

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        />

步骤-2-在您的Java文件中

使用此类导入com.google.android.gms.maps.SupportMapFragment;

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Log.d("tag", "onMapReady");
        }
    });
mapFragment.getMapAsync( this);

希望它对您有帮助。

答案 2 :(得分:0)

我有一些问题。尝试使用普通的FrameLayout并通过代码添加片段。