Shahek Shahek - 1 year ago 750
Android Question

getMapAsync in Fragment

I am using Sliding Tabs with different fragments. In map fragment I cannot use Nested Fragment as XML. Below is Java code and XMLfile.
I am stuck at

method So How Can I get Map using
without Any exception? I will really appreciate your cooperation.

Food Fragment Java:

public class FoodFragment extends Fragment {

private SupportMapFragment mapfragment;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.food_layout, null);

mapfragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(;
if (mapfragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mapfragment = SupportMapFragment.newInstance();
mapfragment.getMapAsync((OnMapReadyCallback) getActivity().getApplicationContext());

return view;


Food XML file,



Answer Source

Your ApplicationContext cannot be casts as OnMapReadyCallback you can create new instance of OnMapReadyCallback() as an anonymous class instead like here is what you might be searching for

  mapfragment.getMapAsync(new OnMapReadyCallback() {
        @Override public void onMapReady(GoogleMap googleMap) {
            if (googleMap != null) {
               // your additional codes goes here

You can refer this example as well if you need.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download