更改导航时地图片段崩溃

时间:2014-01-24 15:09:16

标签: android android-maps-v2 supportmapfragment

我有一个导航抽屉,其中Map是其他项目中的一个选项。当我点击我的地图时,它有效。如果我再次转到其他导航抽屉项目并返回地图,它会崩溃。谁能告诉我原因?

这是代码..

public class Map extends Fragment {
private GoogleMap mMap;

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

    return view;
}

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

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

}

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

private void setUpMapIfNeeded() {

    if (mMap == null) {

        FragmentManager fm = getFragmentManager();
        mMap = ((SupportMapFragment) fm.findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    LatLng latlang = new LatLng(53.0847558, 8.8208279);

    mMap.addMarker(new MarkerOptions().position(latlang).title("Marker"));

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlang, 15));

    mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}}

这是xml

<fragment xmlns:android="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" />

这是我的错误日志消息

01-24 16:00:16.335: E/AndroidRuntime(17656): FATAL EXCEPTION: main
01-24 16:00:16.335: E/AndroidRuntime(17656): android.view.InflateException: Binary   
XML file line #3: Error inflating class fragment
01-24 16:00:16.335: E/AndroidRuntime(17656):    at  
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at   
de.apps28.com.Map.onCreateView(Map.java:27)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at

01-24 16:00:16.335: E/AndroidRuntime(17656): Caused by:    
java.lang.IllegalArgumentException: Binary XML file line #3: Duplicate id 0x7f050011, 
tag null, or parent id 0x0 with another fragment for 
com.google.android.gms.maps.SupportMapFragment
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
01-24 16:00:16.335: E/AndroidRuntime(17656):    at 
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-24 16:00:16.335: E/AndroidRuntime(17656):    ... 18 more

1 个答案:

答案 0 :(得分:6)

问题是,一旦导航到另一个不包含地图的邻居选项卡的选项卡,视图就会被破坏。这会导致堆栈跟踪中的以下行:

  

二进制XML文件行#3:重复ID 0x7f050011,   标记null,或父id为0x0,其中包含另一个片段   com.google.android.gms.maps.SupportMapFragment

所以你试图重新创建地图。更好的解决方案是在视图被销毁之前保存视图

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