【问题标题】:Inner Map Fragment toruble when outer fragment called second time外部片段第二次调用时内部地图片段
【发布时间】:2015-02-23 14:20:46
【问题描述】:

当我第一次调用片段时我可以访问地图,但是当我第二次调用片段时,它给了我这个由错误引起的:

12-26 09:36:41.306: E/AndroidRuntime(28156): Caused by: java.lang.IllegalArgumentException: Binary XML file line #164: Duplicate id 0x7f090012, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment

我在片段类的 onCreateView() 中创建内部地图片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    View view = inflater.inflate(R.layout.howtogo_fragment, container,
            false);

    if(map==null)
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}

这是我的片段的布局:

<LinearLayout>

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

</LinearLayout>

【问题讨论】:

  • map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
  • 在片段的onDestroyView()中移除地图片段。并在 onCreateView 中重新初始化地图。不喜欢添加片段事务。
  • #Figen Güngör 你解决了吗?

标签: android google-maps android-fragments dictionary fragment


【解决方案1】:

你可以试试我的代码: xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.example.catalyst_home.GeolocationActivity$PlaceholderFragment" >

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

片段java:

private GoogleMap mMap;
private SupportMapFragment mMapFragment;
private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getChildFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.map_root, mMapFragment);
        fragmentTransaction.commit();
    }
}

在片段的onResum中:

@Override
public void onResume() {
    super.onResume();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            mMap = mMapFragment.getMap();
            if (mMap != null) {
                mMap.setMyLocationEnabled(true);
            }
        }
    }, 2000);
}

【讨论】:

    【解决方案2】:

    试试这个。

    private static GoogleMap mMap;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
            view = (RelativeLayout) inflater.inflate(R.layout.howtogo_fragment, container, false);
    
                   MapLoad(); // For setting up the MapFragment
    
            return view;
        }
    
        /***** Sets up the map if it is possible to do so *****/
        public static void MapLoad() {
            // Do a null check to confirm that we have not already instantiated the map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) YOURACTIVITY.fragmentManager
                        .findFragmentById(R.id.map)).getMap();
                // Check if we were successful in obtaining the map.
                if (mMap != null)
                    setUpMap();
            }
        }
    private static void setUpMap() {
        // For showing a move to my loction button
        mMap.setMyLocationEnabled(true);
        // For dropping a marker at a point on the Map
      //  mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"));
        // For zooming automatically to the Dropped PIN Location
      //  mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
            //    longitude), 12.0f));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多