Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

前端 未结 23 1630
-上瘾入骨i
-上瘾入骨i 2020-11-22 00:25

I have an application with three tabs.

Each tab has its own layout .xml file. The main.xml has its own map fragment. It\'s the one that shows up when the application

相关标签:
23条回答
  • 2020-11-22 01:08

    Have you been trying to reference your custom MapFragment class in the layout file?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapFragment"
            android:name="com.nfc.demo.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 01:09

    I had the same issue and was able to resolve it by manually removing the MapFragment in the onDestroy() method of the Fragment class. Here is code that works and references the MapFragment by ID in the XML:

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        MapFragment f = (MapFragment) getFragmentManager()
                                             .findFragmentById(R.id.map);
        if (f != null) 
            getFragmentManager().beginTransaction().remove(f).commit();
    }
    

    If you don't remove the MapFragment manually, it will hang around so that it doesn't cost a lot of resources to recreate/show the map view again. It seems that keeping the underlying MapView is great for switching back and forth between tabs, but when used in fragments this behavior causes a duplicate MapView to be created upon each new MapFragment with the same ID. The solution is to manually remove the MapFragment and thus recreate the underlying map each time the fragment is inflated.

    I also noted this in another answer [1].

    0 讨论(0)
  • 2020-11-22 01:10

    Declare SupportMapFragment object globally

        private SupportMapFragment mapFragment;
    

    In onCreateView() method put below code

    mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
    

    In onDestroyView() put below code

    @Override
    public void onDestroyView() {
       super.onDestroyView();
    
        if (mapFragment != null)
            getFragmentManager().beginTransaction().remove(mapFragment).commit();
    }
    

    In your xml file put below code

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

    Above code solved my problem and it's working fine

    0 讨论(0)
  • 2020-11-22 01:10

    I had this in viewPager and the crash was because any fragment had to have its own tag, duplicate tags or ids for same fragment are not allowed.

    0 讨论(0)
  • 2020-11-22 01:10

    Anyone coming here now that's getting this type of error when opening a Dialog or other Fragment with the Google Places AutocompleteSupportFragment, try this one-liner (I don't know how safe this is but it works for me):

    autocompleteFragment.getFragmentManager().beginTransaction().remove(autocompleteFragment).commit();

    before you dismiss/destroy your fragment.

    0 讨论(0)
  • 2020-11-22 01:12
    <?xml version="1.0" encoding="utf-8"?>
    
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
       android:layout_height="match_parent" >
    
    
    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="15dp" >
    </com.google.android.gms.maps.MapView>
    

    Why don't you insert a map using the MapView object instead of MapFragment ? I am not sure if there is any limitation in MapView,though i found it helpful.

    0 讨论(0)
提交回复
热议问题