IllegalStateException: Can't change container ID of Fragment

后端 未结 10 1681
孤街浪徒
孤街浪徒 2020-11-29 04:18

Android platform: 3.1

I am trying to move a fragment from a container A to a container B. Here follows the code to accomplish this:

private void rea         


        
10条回答
  •  春和景丽
    2020-11-29 05:09

    This can happen when try to add the same fragment more than once.

        public void populateFragments() {
            Fragment fragment = new Fragment();
            //fragment is added for the first time
            addFragment(fragment);
    
            // fragment is added for the second time
            // This call will be responsible for the issue. The 
            addFragment(fragment);
        }
    
        public void addFragment(Fragment fragment) {
            FrameLayout frameLayout = AppView.createFrameLayout(context);
            view.addView(frameLayout);
            getSupportFragmentManager().beginTransaction().add(frameLayout.getId(), fragment).commit();
        }
    

提交回复
热议问题