Fragment 内嵌Fragment问题——Duplicate id tag null or parent id 0x0 错误解决方案

来源:互联网 发布:想做淘宝客服 编辑:程序博客网 时间:2024/04/30 01:26

在Fragment中如果内嵌了Fragment,那么在多次点击后内嵌的Fragment会遭成上述的问题Duplicate id  tag null or parent id 0x0,因此我们需要在Fragment的OnDestroyView方法中销毁内嵌的Fragment,这样就可以解决上述的问题,具体代码如下:

 

public class CopyOfSampleTitlesTriangle extends CopyOfBaseSampleFragment {  private UserProfileFragment userProfileFragment;  @Overridepublic void onDestroyView() {  // TODO Auto-generated method stub  super.onDestroyView(); //内嵌的Fragment UserProfileFragment   UserProfileFragment userProfileFragment1 =(UserProfileFragment)getFragmentManager().findFragmentById(R.id.fr_user_profile_user_data_fragment);  if(userProfileFragment1 !=null ){   getFragmentManager().beginTransaction().remove(userProfileFragment1).commit();  }@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {  // TODO Auto-generated method stub  //return super.onCreateView(inflater, container, savedInstanceState);  View view = inflater.inflate(R.layout.fr_user_profile, null);  mAdapter = new TestFragmentAdapter(getChildFragmentManager());        mPager = (ViewPager)view.findViewById(R.id.pager);        mPager.setAdapter(mAdapter);        TitlePageIndicator indicator = (TitlePageIndicator)view.findViewById(R.id.viewpager_indicator);        indicator.setViewPager(mPager);        indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);        mIndicator = indicator;                final float density = getResources().getDisplayMetrics().density;        //indicator.setBackgroundColor(0x18FF0000);                indicator.setFooterColor(Color.WHITE);        indicator.setFooterLineHeight(1 * density); //1dp        indicator.setFooterIndicatorHeight(3 * density); //3dp        //indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);        //indicator.setTextColor(0xAA000000);        //indicator.setSelectedColor(0xFF000000);        indicator.setSelectedBold(true);                userProfileFragment =(UserProfileFragment)getFragmentManager().findFragmentById(R.id.fr_user_profile_user_data_fragment);                //userProfileFragment =(UserProfileFragment)getChildFragmentManager().findFragmentById(R.id.fr_user_profile_user_data_fragment);        User user=new User();        user.fullName="test";        user.description="test description";        user.email="test@gmail.com";        user.isFollowing=true;        user.shortName="test";               userProfileFragment.setUser(user);           return view; } }


2 0
原创粉丝点击