getSupportFragmentManager()在acivtiy扩展片段中未定义

时间:2013-12-13 11:10:29

标签: java android google-maps fragment

我正在尝试在操作栏的标签中实现地图。但要做到这一点,我需要将FragmentActivity改为Fragment。现在,要实现谷歌地图v2,我无法获得地图片段的ID。 这是我的代码中的代码段:

xml文件:     

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

</LinearLayout>

java文件:

public class FragmentMap extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


public static class MapAct extends Fragment {

static final LatLng MARK = new LatLng(53.558678, 9.9271234);

    private GoogleMap map;
    MapView mMapView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.addition, container, false);     
        return v;
    }

     @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            map = ((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            Marker hamburg = map.addMarker(new MarkerOptions().position(MARK));
  }
}

请帮助。我是这个概念的新手。

2 个答案:

答案 0 :(得分:3)

哟需要将Activity转换为FragmentActivity

FragmentActivity activity = (FragmentActivity) getActivity();
map = ((SupportMapFragment)activity.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

答案 1 :(得分:0)

getSupportFragmentManager()位于android.support.v4.app.FragmentActivity的{​​{1}},SupportLibrary (appcompat project)位于android-root\sdk\extras\android\support\v7\appcompat。如果它不可用,那么可以从Android SDK Manager - “附加/ Android支持库”轻松下载。