【问题标题】:How to fix 'Incovertible types' in android studio?如何修复 android studio 中的“不可转换类型”?
【发布时间】:2019-09-17 19:22:19
【问题描述】:

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

package com.dhami.ajay.nearby;

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

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

这行给了我不兼容的类型问题:

_SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)_ 

【问题讨论】:

  • 添加您的代码sn-p,没有足够的信息来回答您的问题。
  • @Ikazuchi 我现在在遇到问题的地方添加了代码

标签: java android android-studio


【解决方案1】:

您遇到的错误意味着 ID 为“map”的片段被定义为常规片段。可能是您的片段在布局中缺少android:name="com.google.android.gms.maps.SupportMapFragment"。检查以确保您的 XML 布局包含如下内容:

<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=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />

旁注:

您使用的似乎是 Google 提供的 Tutorial for the Maps SDK for Android。默认情况下,res/layout/activity_maps.xml 应该包含确切的片段块,因此除非您修改布局或实现自己的布局,否则您应该没有任何问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多