1

I'm having problems with a Map Fragment. The error message is saying Cannot cast from Fragment to MapFragment. I tried changing to extends FragmentActivity together with GoogleMap map = ((SupportMapFragment) getSupportFragmentManager() but that caused problems with my onCreateView line of code instead.

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Fragment01 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_01, container, false);

    // Get a handle to the Map Fragment
    GoogleMap map = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));
}

}

2 Answers2

5

replace

getFragmentManager

with

getSupportFragmentManager

getFragmentManager returns the instance of androids api fragment(available from v11+) class but you have imported android.support.v4.app.Fragment.

So either call getSupportFragmentManager or change inports.

getSupportFragmentManager is available only in FragmentActivity class.

So you need to first put this fragment inside a FragmentActivity (extent FragmentActivity not Activity) and then in onActivityCreated method of fragment get the activity reference and call getSupportFragmentManager. e.g.

myContext.getSupportFragmentManager(); 

for ref.: How can I access getSupportFragmentManager() in a fragment?

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Yes, I have tried that and I get an error saying *The method getSupportFragmentManager() is undefined for the type Fragment01*? –  May 29 '14 at 13:23
  • check my edit. Google "getsupportfragmentmanager in fragment" to know more about how to do it. – vipul mittal May 29 '14 at 13:28
  • Unfortunately, I can't use the `FragmentActivity` class, only the `Fragment` class, otherwise my the `onSelectFragment` section in my `MainActivity`gets all messed up. I've found another solution which I'm currently testing. –  May 29 '14 at 13:41
4

Try these lines of code, they may help you:

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
supportMapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
Google_Map = supportMapFragment.getMap();
Community
  • 1
  • 1