Android在活动视图中附加片段获取片段已添加错误

Posted

技术标签:

【中文标题】Android在活动视图中附加片段获取片段已添加错误【英文标题】:Android attching fragment inside activity view get Fragment already added Error 【发布时间】:2017-04-09 10:36:07 【问题描述】:

在我的活动中,我有简单的 FrameLayout 小部件,我试图通过 replace 将简单片段附加到该小部件上,并将一些片段使用到嵌套片段中,但是当我尝试在其中添加一些片段时出现 Fragment already added 错误嵌套片段,例如这是我的活动:

public class MainActivity extends AppCompatActivity 

    private CarouselFragment carouselFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...

        if (savedInstanceState == null) 
            initScreen();

         else 
            carouselFragment = (CarouselFragment) getSupportFragmentManager().getFragments().get(0);
        

        ...
    

    private void initScreen() 
        carouselFragment = new CarouselFragment();

        final FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, carouselFragment)
                .commit();
    

嵌套片段是CarouselFragment类:

在这个片段中,我试图将其他片段设置为适配器以使用它们

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_carousel, container, false);

    ButterKnife.bind(this, view);

    context = getContext();
    viewPager = (ViewPagerCustomDuration) view.findViewById(R.id.vp_pages);

    adapter = new CarouselViewPagerAdapter(getChildFragmentManager());

    adapter.addFragment(new FragmentA());
    adapter.addFragment(new FragmentB());

    viewPager.setAdapter(adapter);

    viewPager.setPageMargin(50);
    viewPager.setOffscreenPageLimit(3);
    viewPager.setScrollDurationFactor(4);
    viewPager.setOffscreenPageLimit(1);

    return view;


@Override
public void onActivityCreated(Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);


然后这是我的适配器:

public class CarouselViewPagerAdapter extends FragmentStatePagerAdapter 
    private final List<Fragment> mFragmentList = new ArrayList<>();

    public CarouselViewPagerAdapter(FragmentManager manager) 
        super(manager);
    

    @Override
    public Fragment getItem(int position) 
        return mFragmentList.get(position);
    

    @Override
    public int getCount() 
        return mFragmentList.size();
    

    public void addFragment(Fragment fragment) 
        mFragmentList.add(fragment);
    

    @Override
    public CharSequence getPageTitle(int position) 
        return null;
    

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        mFragmentList.add(position, fragment);
        return fragment;
    

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) 
        mFragmentList.remove(position);
        super.destroyItem(container, position, object);
    

    public Fragment getRegisteredFragment(int position) 
        return mFragmentList.get(position);
    

问题是这行代码:

    adapter.addFragment(new FragmentA());
    adapter.addFragment(new FragmentB());

【问题讨论】:

检查解决方案。 here 不完全是您的问题,但您可能可以弄清楚可能是什么问题? 【参考方案1】:

您只能替换通过FragmentTransaction 动态添加的片段。

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);  
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit(); 

尝试使用教程:-http://sapandiwakar.in/replacing-fragments/

看看这个:- Replacing a fragment with another fragment inside activity group

希望这会对你有所帮助。

【讨论】:

***.com/questions/6250580/… 这可能会对您有所帮助。 我已经测试过这个解决方案,但是当我将我的主要片段用作CarouselFragment 时不要调用并且不要运行

以上是关于Android在活动视图中附加片段获取片段已添加错误的主要内容,如果未能解决你的问题,请参考以下文章

Android获取活动中片段的视图[重复]

在片段中重新加载android视图

可扩展列表视图在 android 的片段活动中不起作用

在片段中,如何查找活动布局中定义的视图?

片段事务分离和附加后ListView不工作?

android如何跨片段分离/附加保留视图状态