2016-07-28 130 views
9

我試圖在使用getChildFragmentManager()的viewpager內的另一個片段內添加一個片段。我得到了以下錯誤,ChildFragmentManager java.lang.IllegalStateException:活動已被破壞

java.lang.IllegalStateException:活動已在android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1549) 被破壞 在android.support.v4.app .BackStackRecord.commitInternal(BackStackRecord.java:654) 在android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:625) 在com.lakeba.gameon.userprofile.UserProfileContainerFragment.replaceFragment(UserProfileContainerFragment.java:72 )

而我嘗試this workaround但仍然收到相同的錯誤。

UserProfileContainerFragment.java

public class UserProfileContainerFragment extends CustomFragment { 


    private View rootView; 
    private Fragment fragment1; 

    public UserProfileContainerFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRetainInstance(true); 
    } 

    public static UserProfileContainerFragment newInstance() { 
     UserProfileContainerFragment fragment = new UserProfileContainerFragment(); 
     return fragment; 
    } 


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

     if(savedInstanceState == null) { 
      UserProfileFragment userProfileFragment = UserProfileFragment.newInstance(); 
      getChildFragmentManager().beginTransaction() 
        .replace(R.id.user_profile_container, userProfileFragment) 
        .commitAllowingStateLoss(); 
     } 

     return rootView; 
    } 

    /*@Override 
    public void onSaveInstanceState(Bundle outState) { 
     //super.onSaveInstanceState(outState); 
    }*/ 

    public void replaceFragment(Fragment fragment, boolean addToBackStack){ 
     fragment1 = fragment; 
     if(addToBackStack){ 
      /*getChildFragmentManager().beginTransaction() 
        .replace(R.id.user_profile_container, fragment) 
        .addToBackStack(null) 
        .commit();*/ 
      getChildFragmentManager().beginTransaction() 
        .replace(R.id.user_profile_container, fragment) 
        .addToBackStack(null) 
        .commit(); 
        //.commitAllowingStateLoss(); 
     } 
     else{ 
      getChildFragmentManager().beginTransaction() 
        .replace(R.id.user_profile_container, fragment) 
        .commit(); 
        //.commitAllowingStateLoss(); 
     } 

    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 

     try { 
      Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); 
      childFragmentManager.setAccessible(true); 
      childFragmentManager.set(this, null); 

     } catch (NoSuchFieldException e) { 
      throw new RuntimeException(e); 
     } catch (IllegalAccessException e) { 
      throw new RuntimeException(e); 
     } 

    } 
} 

HomeMapActivity.java

public class HomeMapActivity extends AppCompatActivity implements UserProfileFragment.OnUserProfileFragmentListener{ 

    private Toolbar homeToolbar; 
    private ViewPager homeViewPager; 
    private TabLayout homeTabLayout; 
    private UserProfileContainerFragment userProfileContainerFragment; 

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

     /*homeToolbar = (Toolbar) findViewById(R.id.home_toolbar); 
     setSupportActionBar(homeToolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/ 

     homeViewPager = (ViewPager) findViewById(R.id.home_view_pager); 

     HomeViewPagerAdapter homeViewPagerAdapter = new HomeViewPagerAdapter(getSupportFragmentManager()); 
     userProfileContainerFragment = new UserProfileContainerFragment(); 
     homeViewPagerAdapter.addFragment(new UserProfileContainerFragment(),"Profile"); 
     homeViewPager.setAdapter(homeViewPagerAdapter); 

     homeTabLayout = (TabLayout) findViewById(R.id.home_tabs); 
     homeTabLayout.setupWithViewPager(homeViewPager); 
     setTabIcons(homeTabLayout); 


     homeTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       int tabPosition = tab.getPosition(); 
       tab.setIcon(tabIconsArrayActivated[tabPosition]); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 
       int tabPosition = tab.getPosition(); 
       tab.setIcon(tabIconsArray[tabPosition]); 
      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 
       int tabPosition = tab.getPosition(); 
       tab.setIcon(tabIconsArrayActivated[tabPosition]); 
      } 
     }); 

     homeTabLayout.getTabAt(1).select(); 
    } 

    private void setTabIcons(TabLayout homeTabLayout) { 
     homeTabLayout.getTabAt(0).setIcon(tabIconsArray[0]); 
     homeTabLayout.getTabAt(1).setIcon(tabIconsArray[1]); 
     homeTabLayout.getTabAt(2).setIcon(tabIconsArray[2]); 
     homeTabLayout.getTabAt(3).setIcon(tabIconsArray[3]); 
    } 

    @Override 
    public void onUserProfileEditButtonClicked() { 
     userProfileContainerFragment.replaceFragment(EditUserProfileFragment.newInstance(),true); 
    } 

    private class HomeViewPagerAdapter extends FragmentStatePagerAdapter{ 

     private final List<Fragment> mFragmentList = new ArrayList<>(); 
     private final List<String> mFragmentTitleList = new ArrayList<>(); 

     public HomeViewPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

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

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      //return mFragmentTitleList.get(position); 
      return null; 
     } 

     public void addFragment(Fragment fragment,String title){ 
      mFragmentList.add(fragment); 
      mFragmentTitleList.add(title); 
     } 
    } 
} 
+0

這個問題的任何答案嗎? – Ramprasad

+0

https://github.com/rramprasad/TabsSample。這是我的github回購此示例來重現此問題。 – Ramprasad

回答

5

如果你看看你的onCreate()方法中的代碼,你會看到線:

userProfileContainerFragment = new UserProfileContainerFragment(); 
homeViewPagerAdapter.addFragment(new UserProfileContainerFragment(),"Profile"); 

您首先使用UserProfileContainerFragment的新實例初始化userprofileContainerFragment字段,然後在您的下面創建一個新的 UserProfileContainerFragment的實例,以便在ViewPager中使用(您可以通過在UserProfileContainerFragment的構造函數中放置一條日誌語句來輕鬆觀察該實例。你會看到兩個實例被創建)。稍後在您的代碼中,您嘗試使用字段userProfileContainerFragment,這將導致失敗,因爲UserProfileContainerFragment的實例根本沒有附加到活動(這不是ViewPager使用的片段)。

您的代碼應該如下,保持適當的參考,而不是建立分離的片段:

userProfileContainerFragment = new UserProfileContainerFragment(); 
homeViewPagerAdapter.addFragment(userProfileContainerFragment,"Profile"); 
+1

太棒了!愚蠢的錯誤在過去的兩週裏吃了很多時間! – Ramprasad

相關問題