Cannot resolve method “getMapAsync”

▼魔方 西西 提交于 2020-07-08 19:48:48

问题


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:

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!


回答1:


You have to use SupportMapFragment rather than MapFragment
in 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);


来源:https://stackoverflow.com/questions/42166972/cannot-resolve-method-getmapasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!