2013-03-28 38 views
1

爲什麼這會返回「片段已添加」錯誤並導致應用程序崩潰?獲取錯誤:片段已添加?

android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 

    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); 



    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); 
    ft.replace(R.id.fragment_container, tempmainfrag); 
    ft.commit(); 
    for(int i = 0; i < fm.getBackStackEntryCount(); i++) 
    { 
     fm.popBackStack(); 
    } 

我猜這是因爲for循環,因爲它的工作原理沒有它,但我需要清除後退堆棧,我怎麼能正確地做到這一點?

+0

Stacktrace請 – Warpzit 2013-03-28 07:12:45

回答

1

與mapfragments工作時,我有同樣的錯誤,這是最終解決了這對我來說:

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

    SupportMapFragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.current_fragment); 
    if (f != null) 
     getFragmentManager().beginTransaction().remove(f).commit(); 
} 

希望它能幫助。