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

在Fragment中假设内嵌了Fragment,那么在多次点击后内嵌的Fragment会遭成上述的问题Duplicate id  tag null or parent id 0x0。因此我们须要在Fragment的OnDestroyView方法中销毁内嵌的Fragment。这样就能够解决上述的问题。详细代码例如以下:

 

public class CopyOfSampleTitlesTriangle extends CopyOfBaseSampleFragment {
 
 private UserProfileFragment userProfileFragment;
 
 @Override

public 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;
 }


 }


posted on 2019-05-13 14:16  xfgnongmin  阅读(367)  评论(0编辑  收藏  举报

导航