简体   繁体   English

如何获取谷歌地图API从android.app.Fragment扩展而不是Fragment Activity

[英]How to get google maps api to extend from android.app.Fragment instead of Fragment Activity

The problem i have is I'm trying to get google maps api working in my app. 我遇到的问题是我正在尝试使Google Maps api在我的应用程序中工作。 had it working before but i have since added the navigation drawer to my app which uses a different type of fragment android.app.Fragment and the map uses FragmentActivity when i add the code to the fragment class and extend android.app.Fragment i get the following errors . 以前有它工作过,但后来我将导航抽屉添加到我的应用程序中,该应用程序使用另一种类型的片段android.app.Fragment ,当我向片段类添加代码并扩展android.app.Fragment时,地图使用FragmentActivity以下errors

Error:(46, 63) error: cannot find symbol method getSupportFragmentManager() 错误:(46,63)错误:找不到符号方法getSupportFragmentManager()

Error:(66, 48) error: incompatible types: MyMapFragment cannot be converted to Context 错误:(66、48)错误:类型不兼容:MyMapFragment无法转换为上下文

Error:(66, 171) error: incompatible types: MyMapFragment cannot be converted to Context 错误:((66,171)错误:类型不兼容:MyMapFragment无法转换为上下文

Here is my Fragment class for the map 这是我的地图片段类

public class MyMapFragment extends android.app.Fragment implements OnMapReadyCallback {

private GoogleMap mMap;

public MyMapFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_map, container, false);

    return view;

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

Heres the fragment_map.xml 这是fragment_map.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    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="wit.ie.mightyangler.Fragment.MapFragment" />

Use not support version 使用不支持的版本

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

and in xml 并在xml中

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM