0

我有一个问题和一个问题。首先我不知道这是否可能:
我有一个带有标签的片段活动。在一个选项卡(这是一个片段)上,我有一张带有隐藏项目列表的地图。地图被放入该片段的框架中。列表已下载,按下按钮再次可见。在地图上,我有代表这些项目的标记。 我的问题是这样的:当我从我放入框架的片段中 使用时,
我总是得到地图。这可能吗?如果不是,你有什么推荐?提前致谢。nullgetMap()


已编辑

public class MapExplore extends Fragment{
private FrameLayout frame;
    private ListView list;

    private String api_key;

    private GoogleMap map;
    private MapExploreAdapter adapter;

    private SupportMapFragment map_fragment;
@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        api_key = Configuration.get_prefereence_string(getActivity(), "user_activation_key", null);

        adapter = new MapExploreAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_explore, null);
        frame = (FrameLayout) view.findViewById(R.id.frameMap);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setUpMapFragment();
    }

    private void setUpMapFragment(){
        if(map_fragment == null){
            map_fragment = SupportMapFragment.newInstance();
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.add(frame.getId(), map_fragment, Utils.MAP_FRAGMENT_TAG);
            ft.commit();

        }
    }

    private void setUpMap(){
        Log.i("SET UP MAP", "Started");
        if(map == null){
            map = ((SupportMapFragment) map_fragment).getMap();
        }
        if(map != null){
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Utils.SF_LAT, Utils.SF_LON), 13));
        }       
    }
@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setUpMap();
    }

我用一些代码编辑了我的问题......

4

1 回答 1

3

如果您可以发布一些代码,则更容易诊断您的问题:

同时尝试如下动态创建您的 SupportMap 片段:

mMapFragment = new SupportMapFragment() {
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                GoogleMap map = mMapFragment.getMap();
                if (map != null) {
                    //Your initialization code goes here
                }
            }
        };

在片段内使用 Mapview 的替代方法如下:

public class Yourfragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
}

并将地图视图放入 XML 中,如下所示:

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

这是MapView官方文档的链接

于 2013-02-28T15:02:03.617 回答