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

前端 未结 23 1562
-上瘾入骨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:15

    You are returning or inflating layout twice, just check to see if you only inflate once.

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

    I respect all the answers but i found this one liner solution: If n Is the Number of tabs then:

     mViewPager.setOffscreenPageLimit(n);
    

    Example: In case mentioned :

     mViewPager.setOffscreenPageLimit(2);
    

    View pager implements a queue so, you don't have to let it remove that fragment. onCreateView is called only once.

    0 讨论(0)
  • 2020-11-22 01:20
    1. As mentioned by @Justin Breitfeller, @Vidar Wahlberg solution is a hack which might not work in future version of Android.
    2. @Vidar Wahlberg perfer a hack because other solution might "cause the map to be recreated and redrawn, which isn't always desirable". Map redraw could be prevented by maintaining the old map fragment, rather than creating a new instance every time.
    3. @Matt solution doesn't work for me (IllegalStateException)
    4. As quoted by @Justin Breitfeller, "You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically."

    My solution:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map_list, container, false);
    
        // init
        //mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        // don't recreate fragment everytime ensure last map location/state are maintain
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            mapFragment.getMapAsync(this);
        }
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        // R.id.map is a layout
        transaction.replace(R.id.map, mapFragment).commit();
    
        return view;
    }
    
    0 讨论(0)
  • 2020-11-22 01:21

    Try setting an id (android:id="@+id/maps_dialog") for your mapView parent layout. Works for me.

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

    Another solution:

    if (view == null) {
        view = inflater.inflate(R.layout.nearbyplaces, container, false);
    }
    

    That's it, if not null you don't need to reinitialize it removing from parent is unnecessary step.

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

    If you will use only Vidar Wahlberg answer, you get error when you open other activity (for example) and back to map. Or in my case open other activity and then from new activity open map again( without use back button). But when you combine Vidar Wahlberg solution and Matt solution you will have not exceptions.

    layout

    <com.example.ui.layout.MapWrapperLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/map_relative_layout">
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/root">
    
            <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" />
        </RelativeLayout>
    </<com.example.ui.layout.MapWrapperLayout>
    

    Fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null){
                parent.removeView(view);
            }
        }
        try {
            view = inflater.inflate(R.layout.map_view, null);
            if(view!=null){
                ViewGroup root = (ViewGroup) view.findViewById(R.id.root);
    ...
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = this.getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        if (fragment != null)
            getFragmentManager().beginTransaction().remove(fragment).commit();
    }
    
    0 讨论(0)
提交回复
热议问题