替换FragmentPagerAdapter中的部分片段

时间:2013-10-18 22:23:55

标签: android android-fragments fragment fragmentpageradapter fragment-tab-host

我有一个FragmentPagerAdapter,其中有几个片段在运行时根据用户设置动态创建。

根据这些用户设置,每个片段的一部分应该被另一个片段替换(有时候应该在片段中添加进度条,有时不会。)

这是我的替代代码:

    OverviewProgressElementFragment fragment = new OverviewProgressElementFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.pBarContainer, fragment);
    ft.commit();

问题是,此行FragmentTransaction ft = getFragmentManager().beginTransaction();会导致此异常:

10-18 17:51:57.822: E/AndroidRuntime(1877): FATAL EXCEPTION: main
10-18 17:51:57.822: E/AndroidRuntime(1877): java.lang.NullPointerException
10-18 17:51:57.822: E/AndroidRuntime(1877):     at com.xxx.xxx.OverviewEntityFragment.SetEntity(OverviewEntityFragment.java:119) <- the method in which I call the transaction
10-18 17:51:57.822: E/AndroidRuntime(1877):     at com.xxx.xxx.PageAdapter.getItem(PageAdapter.java:27)
10-18 17:51:57.822: E/AndroidRuntime(1877):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
10-18 17:51:57.822: E/AndroidRuntime(1877):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
10-18 17:51:57.822: E/AndroidRuntime(1877):     at android.support.v4.view.ViewPager.populate(ViewPager.java:982)

我的层次结构如下:

  1. 包含FragmentTabHost的MainActivity

  2. 包含ViewPager的一个标签 - &gt; FragmentPagerAdapter - &gt;片段

  3. 应动态部分加载'子碎片'的碎片

  4. 当片段试图替换内容时,第3部分会发生错误。

    以下是FragmentPagerAdapter中片段的创建代码:

    @Override
    public Fragment getItem(int position) 
    {
        OverviewEntityFragment oeFragment = new OverviewEntityFragment();
        oeFragment.SetEntity(_entityList.get(position));
        return oeFragment;
    }   
    

    SetEntity() - 方法中发生异常。这是代码:

    public void SetEntity(OverviewEntityData entityData)
    {
    _entityData = entityData;
    
     if ((_entityData.getEntityType() != ENTITY_TYPE.MEMORY)
     && (_entityData.getEntityType() != ENTITY_TYPE.NO_PAGE_FOUND))
     {
        OverviewProgressElementFragment fragment = new OverviewProgressElementFragment();
        fragment.SetEntityData(_entityData);
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.pBarContainer, fragment);
        ft.commit();            
        }
    }
    

    当我写下来时也会发生异常:

    public void SetEntity(OverviewEntityData entityData)
    {
          FragmentTransaction ft = getFragmentManager().beginTransaction();
    }
    

    是不是可以在这样的层次中替换部分片段,还是我做错了什么?

    非常感谢任何帮助。

    修改

    我尝试使用 getChildFragmentManager() ,但这会引发异常:

    10-18 19:02:48.622: E/AndroidRuntime(2056): FATAL EXCEPTION: main
    10-18 19:02:48.622: E/AndroidRuntime(2056): java.lang.IllegalStateException: Activity has been destroyed
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1358)
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at com.xxx.xxx.OverviewEntityFragment.SetEntity(OverviewEntityFragment.java:121)
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at com.xxx.xxx.PageAdapter.getItem(PageAdapter.java:27)
    10-18 19:02:48.622: E/AndroidRuntime(2056):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
    

1 个答案:

答案 0 :(得分:0)

FragmentActivity内,您应拨打getSupportFragmentManager()而不是getFragmentManager()

在嵌套Fragment来电getChildFragmentManager()

编辑:

创建片段时:

OverviewEntityFragment oeFragment = new OverviewEntityFragment();

此片段尚未附加到Activity,因此例外。

我猜你可以将FragmentManager传递给SetEntity()方法,但我不确定这会怎样。