gpt4 book ai didi

java - 如何修复android studio中的 'Incovertible types'?

转载 作者:行者123 更新时间:2023-12-02 09:44:53 24 4
gpt4 key购买 nike

不可转换的类型;无法将“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)_ 

最佳答案

您收到的错误意味着 ID 为“map”的 fragment 被定义为常规 fragment 。可能是您的 fragment 在布局中缺少 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" />
<小时/>

旁注:

您似乎正在使用 Tutorial for the Maps SDK for Android由谷歌提供。默认情况下,res/layout/activity_maps.xml 中应包含确切的 fragment block ,因此除非您修改布局或实现自己的布局,否则不会出现任何问题。

关于java - 如何修复android studio中的 'Incovertible types'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56738747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com