0

我有一個FragmentPagerAdapter,其中有幾個片段,這些片段在運行時根據用戶設置而動態創建。替換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. MainActivity FragmentTabHost

  2. > FragmentPagerAdapter - - 含ViewPager
  3. 一個突>片段

  4. 片段應該部分地加載在第3部分 '亞片段' 動態

錯誤occures當片段改掉替換內容。

這裏是我的FragmentPagerAdapter片段創建代碼:

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

例外occures在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();    
    } 
} 

的異常也occures時,我只是寫:

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) 

回答

0

裏面一個FragmentActivity你應該叫getSupportFragmentManager(),而不是getFragmentManager()

而在嵌套Fragment呼籲getChildFragmentManager()

編輯:

當您創建片段:

OverviewEntityFragment oeFragment = new OverviewEntityFragment(); 

該片段尚未連接到Activity,因此例外。

我猜你可以通過FragmentManagerSetEntity()方法,但我不知道如何解決這個問題。

+0

感謝您的回答,但在我的Fragment中使用getChildFragmentManager()會引發另一個異常。我更新了我原來的帖子。 – user1567896