Android - SupportMapFragment with GoogleMaps API 2.0 giving IllegalArgumentException

前端 未结 11 1703
情书的邮戳
情书的邮戳 2020-11-27 04:17

I am trying to use the latest Map API 2.0 provided for Android. I am using the Support Library as I want to support Android 2.2. Following is my code:

Main A

相关标签:
11条回答
  • 2020-11-27 04:53

    You can fix this, if you delete all nested fragments in onDestroyView(). Don't know if it is a proper solution.

    public void onDestroyView() {
       super.onDestroyView(); 
       Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));   
       FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
       ft.remove(fragment);
       ft.commit();
    }
    

    And inflating them as usual in onCreateView()

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.map, container, false);
    }
    
    0 讨论(0)
  • 2020-11-27 04:53

    Use dialog.SetContentView() method in your activity's onCreate() cause when we tring to load.

    Dialog again it loads only dialog not the whole activity life cycle and leads it to exception of Duplicate id.

    Try it.

    0 讨论(0)
  • 2020-11-27 04:53

    I used Natalia response but at times broke the application. Use this one and it worked perfectly without breaking.

    @Override
    public void onDestroyView() {
    
      try{
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
    
        transaction.remove(nestedFragment);
    
        transaction.commit();
      }catch(Exception e){
      }
    
        super.onDestroyView();
    }
    

    https://stackoverflow.com/a/7953801/3364157

    0 讨论(0)
  • 2020-11-27 04:54

    Used this solution (similar to others):

    public void onDestroyView() {
    
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment fragment = (fm.findFragmentById(R.id.map));
    
        if (fragment.isResumed()) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }
        super.onDestroyView();
    }
    

    I had problems before using the if (fragment.isResumed).

    0 讨论(0)
  • 2020-11-27 05:03

    You need to use getChildFragmentManager() to add SupportMapFragment not not through xml.

    For "why", see official documentation: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

    Take a look at my answer here: https://stackoverflow.com/a/15512285/2183804

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