从地图片段切换到另一个片段和回 [英] Switching from Map fragment to another Fragment and back

查看:150
本文介绍了从地图片段切换到另一个片段和回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段的活动,片段切换按钮点击。
这两个片段,我转是:

I have a fragment activity that switches fragments on button click.
The two fragments that I switch are:

  1. 在谷歌地图Android的V2片段
  2. 在一个文本视图另一个片段

这是我怎么切换片段:

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();

        }
    }
}

这是我的地图Fagment code

This s my Maps Fagment code

  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

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"/>

但每次我SWICH回到地图片段的应用程序崩溃。我应该怎样使这个code吧?

But every time I swich back to the Map fragment the Application crashes. How should I make this code right?

推荐答案

我怀疑这是发生在你,因为你在你的XML布局文件,你的地图片段定义。为了交换片段用这种方式,你需要你的实例在code地图,类似如下:

I suspect this is happening to you because you have your map fragment defined in your XML layout file. In order to swap fragments in this way, you will need to instantiate your map in code, something like the following:

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,所以这就是为什么你需要通过安排设置的其余部分处理器实例。我的布局XML如下所示:

I found that mMapFragment.getMap() was returning null, so that's why you'll need to schedule the rest of the setup through a Handler instance. My layout XML looks like the following:

<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>

我希望这有助于。

I hope this helps.

这篇关于从地图片段切换到另一个片段和回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆