getSupportFragmentManager() method is undefined for type MapFragment

房东的猫 提交于 2019-12-03 08:55:39
Sam

If you are using this SherlockMapFragment, try:

getSherlockActivity().getSupportFragmentManager();

Dude, I spent last couple of hours reading this and other 2 of your questions (among others) because I was having exactly same issue. I had an app working ABS+GMapsV2.0+FragmentActivity, but since I hear a lot that it's a good practice to have it all in fragments, I started porting it to fragments and FragmentActivities as containers.

When I ported the map, happened exactly the same, getSupportFragmentManager() was not recognized, and I did not like quite much the proposed solution creating the SherlockMapFragment messing up the ABS library, so I kept digging.

Most likely you knew already at this point, but I just realize you (as well as me 10 minutes ago) were trying to handle a fragment that is defined in xml but is not created (indexed) yet.

Taking a look at documentation checking the order of calls in fragment methods, I tried the code in other method after onCreateView()... and changing getSupportFragmentManager() for getFragmentManager() or even following the solution given using getSherlockActivity().getSupportFragmentManager() I could handle those fragments which already exist.

public class MapsFragment extends SherlockFragment {


private SupportMapFragment mSupportMapFragment;
private GoogleMap map;
private Marker spotAMarker;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.maps_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_fragment_test); //THIS ONE
    mSupportMapFragment = (SupportMapFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map_fragment_test); //OR THIS ONE
    if(mSupportMapFragment != null) {
        map = mSupportMapFragment.getMap(); 

        if(map != null) {

            spotAMarker = map.addMarker(new MarkerOptions().position(new LatLng(4.651904,-74.057352)).
                    title(getString(R.string.spot_a_title)).snippet(getString(R.string.spot_a_subtitle)).
                    icon(BitmapDescriptorFactory.fromAsset("images/spot_a.png")));
            spotAMarker.setDraggable(true);

        } else {
            Toast.makeText(this.getActivity(), getString(R.string.no_map_error), Toast.LENGTH_LONG).show();
        }

    } else {
        Toast.makeText(this.getActivity(), "No Map Fragment", Toast.LENGTH_LONG).show();
    }
    super.onActivityCreated(savedInstanceState);
}
}

May be you got already the workaround for the issue, but I wanted to share my experience with other stucked in same issue.

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