android - 无法解析方法 "getMapAsync"

标签 android google-maps android-studio

如有任何语法错误,请原谅。 我按照教程在 fragment 中查看了谷歌地图,但出了点问题。

这是我的文件 .java,它与 fragment Hook :

public class MapFragment extends Fragment implements OnMapReadyCallback{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    fragment.getMapAsync(this); //Here i get the error
}

@Override
public void onMapReady(GoogleMap googleMap) {

}

这是布局 fragment :

<fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"></fragment>

我做错了什么?

谢谢!

最佳答案

你必须使用 SupportMapFragment 而不是 MapFragment
在 XML 中

<fragment
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"></fragment>

在 fragment 中

SupportMapFragment fragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);

关于android - 无法解析方法 "getMapAsync",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42166972/

相关文章:

javascript - 以编程方式关闭/隐藏/删除 Google map 工具提示

android - 如何使用 gradle 从 maven repo 中正确删除传递依赖

android - 更新到 Android studio 3.5 后出现以下错误 - FAILURE : Build failed with an exception

android - 在 Android Spinner 中更改所选项目的背景颜色

android - 不理解 com.google.android.maps.Overlay 的 drawAt() 方法

android - 仅从 Android 7.0 (Nougat) 开始支持默认接口(interface)方法

android - 在android中滚动谷歌地图v2时获取 map 中心点?

jquery - Google Maps API V3 fitbounds() 缩小但从不放大

android - Gradle 同步时此 normalizeAndCheck(UnixPath.java) NullPointerException 的修复方法是什么?

android - 在嵌套的 HashMap 上 Firestore 多个 whereEqualTo