【问题标题】:Google maps fragment crashes when clicked twice from navigation drawer从导航抽屉中单击两次时,Google 地图片段崩溃
【发布时间】:2014-07-10 18:52:17
【问题描述】:

我有一个包含三个列表项的导航抽屉。第一个显示一个带有文本视图的片段,第二个显示一个带有地图的片段。当我单击选项以显示地图片段时它可以工作,并且当我再次切换到第一个片段时它可以工作,但是当我尝试再次单击地图片段时它会崩溃。

我的地图片段类看起来像:

public class FragmentTwo extends Fragment {

GoogleMap map;
public static Fragment newInstance(Context context) {
    FragmentTwo f = new FragmentTwo();

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null,false);
    if (root != null) {
        ViewGroup parent = (ViewGroup) root.getParent();
        if (parent != null)
            parent.removeView(root);
    }
    try {
        root = inflater.inflate(R.layout.fragment_two, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }

    setUpMapIfNeeded();
    return root;
}

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

@Override
public void onPause() {
    super.onPause();
    setUpMapIfNeeded();

}

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

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (map == null) {
        // Try to obtain the map from the SupportMapFragment.
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (map != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {

    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));


}}

我的 xml 看起来像:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

【问题讨论】:

    标签: android google-maps android-fragments navigation-drawer


    【解决方案1】:

    删除这一行

    View root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null,false);
    

    并在 MainActivity 中进行 Initialze

    FragmentTwo f = new FragmentTwo(); 
    

    在 Oncreate 中 当你需要切换到这个片段时,你可以使用

     FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, f).commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多