【问题标题】:error: incompatible types: Fragment cannot be converted to SupportMapFragment错误:不兼容的类型:Fragment 无法转换为 SupportMapFragment
【发布时间】:2019-06-27 04:21:47
【问题描述】:

enter image description here我想为我的项目添加谷歌地图。我正在尝试这样做然后我遇到了这个问题

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

【问题讨论】:

标签: java android google-maps android-fragments


【解决方案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' 

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个

      第 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 文件中

      使用这个类import 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);
      

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-13
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 2017-05-18
        • 2017-12-28
        • 2019-02-26
        • 1970-01-01
        相关资源
        最近更新 更多