【问题标题】:error: incompatible types: Fragment cannot be converted to SupportMapFragmen错误:不兼容的类型:片段无法转换为 SupportMapFragmen
【发布时间】:2019-11-16 18:18:17
【问题描述】:

我正在尝试创建一个基本的 google maps android 应用程序。 就在我创建项目并获得 API 密钥之后,我收到了这个错误。

“错误:不兼容的类型:Fragment 无法转换为 SupportMapFragment”

从线: SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

Java代码:(一切都是android studio生成的)

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);
        getSupportFragmentManager();
        // 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);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    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=".MapsActivity" />

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    我能够通过从

    更改 gradle 中的依赖项来解决问题
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    

    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多