简体   繁体   English

无法解析方法“getMapAsync”

[英]Cannot resolve method “getMapAsync”

Excuse me for any grammatical errors. 对不起任何语法错误。 I followed a tutorial to view the google map in a fragment, but something has gone wrong. 我按照教程查看片段中的谷歌地图,但出了点问题。

this is my file .java that is hooked with the fragment: 这是我的文件.java与片段挂钩:

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) {

}

} }

This is the layout fragment: 这是布局片段:

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

What did I do wrong? 我做错了什么?

Thank you! 谢谢!

You have to use SupportMapFragment rather than MapFragment 您必须使用SupportMapFragment而不是MapFragment
in XML 在XML中

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

in Fragment 在片段中

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

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

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