从Map片段切换到另一个Fragment并返回

时间:2012-12-27 13:07:02

标签: android google-maps android-fragments

我有一个片段活动,可以在按钮点击时切换片段 我切换的两个片段是:

  1. 谷歌地图android v2片段
  2. 另一个带有文字视图的片段
  3. 这就是我切换片段的方式:

    public void onClick(View v) {
        FragmentManager fm = getSupportFragmentManager();
        if(tv.getText().toString().equalsIgnoreCase("Click ME!!")){
            tv.setText("MAP");
            if (fm != null) {
                if(map == null){    
                    map = new Map();
                }
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.lay_contaier, map);
                ft.commit();
    
            }
        }
        else{
            tv.setText("Click ME!!");
            if (fm != null ){
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.lay_contaier,new empty());
                ft.commit();
    
            }
        }
    }
    

    这是我的地图Fag代码

      public class Map extends Fragment implements OnMarkerClickListener,    OnInfoWindowClickListener, OnMarkerDragListener {
    
    private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
    private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
    
    
      private GoogleMap mMap;
    
    private Marker mPerth;
      private Marker mSydney;
      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_main,null);
    
            return view;
      }
    

    activity_mail.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/map"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     class="com.google.android.gms.maps.SupportMapFragment"/>
    

    但每次我回到Map片段时,应用程序崩溃了。我该怎么做这个代码呢?

2 个答案:

答案 0 :(得分:6)

我怀疑这是因为您在XML布局文件中定义了地图片段。为了以这种方式交换片段,您需要在代码中实例化地图,如下所示:

private void initializeMap() {

    mMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, mMapFragment, "map");
    fragmentTransaction.commit();

    handler.post(new Runnable() {
        @Override
        public void run() {
            mMap = mMapFragment.getMap();

            mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
                @Override
                public void onCameraChange(CameraPosition cameraPosition) {
                    // Do something here
                }
            });
        }
    });
}

我发现mMapFragment.getMap()返回null,因此您需要通过Handler实例安排其余的设置。我的布局XML如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LauncherActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">

    <!-- The fragments will go here -->
</RelativeLayout>

<RelativeLayout
    android:layout_gravity="start"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:background="@color/drawer_background">
...
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

我希望这会有所帮助。

答案 1 :(得分:0)

你正在使FragmentManager Null。删除fm=null。同样在else循环中,您将替换两个片段。不要一次做双重更换。在调用commit()之前,只执行一次替换

相关问题