我有一个带有几个片段的 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)
我的层次结构如下:
MainActivity 包含 FragmentTabHost
一个包含 ViewPager -> FragmentPagerAdapter -> Fragments 的选项卡
应该部分动态加载“子片段”的片段
当片段尝试替换内容时,第 3 部分中会发生错误。
这是我的 FragmentPagerAdapter 中片段的创建代码:
@Override
public Fragment getItem(int position)
{
OverviewEntityFragment oeFragment = new OverviewEntityFragment();
oeFragment.SetEntity(_entityList.get(position));
return oeFragment;
}
异常发生在 -Method 中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)