2012-03-05 44 views
0

我使用sherlock bar(http://actionbarsherlock.com/)。有TabListener代碼:SherlockBar中的片段附加錯誤

public class TabListener implements ActionBar.TabListener { 

    private String mTag; 
    private Fragment mFragment; 


    public TabListener(String tag) { 

     mTag = tag; 


     // Check to see if we already have a fragment for this tab, probably 
     // from a previously saved state. If so, deactivate it, because our 
     // initial state is that a tab isn't shown. 
     mFragment = getSupportFragmentManager().findFragmentByTag(mTag); 
     if (mFragment != null && !mFragment.isDetached()) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.detach(mFragment); 
      ft.commit(); 
     } 
    } 

    public void onTabSelected(Tab tab) { 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     if (mFragment == null) { 
      if((tab.getPosition()==0) || (tab.getPosition()==1)) { 
       mFragment = new MainListFragment(ctx, ONLINE_TYPE); 
      } else { 
       mFragment = new SimpleFragmentf(); 

      } 
      ft.add(com.lib.reader.R.id.root2, mFragment, mTag); 
      ft.addToBackStack(null); 
      ft.commit(); 
     } else { 
      ft.attach(mFragment); 
      ft.commit(); 
     } 
    } 

    public void onTabUnselected(Tab tab) { 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.detach(mFragment); 
     ft.commit(); 
    } 

    @Override 
    public void onTabReselected(Tab tab) { 
     //To change body of implemented methods use File | Settings | File Templates. 
    } 
} 

還有就是查看的代碼部分:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    LinearLayout v = (LinearLayout)inflater.inflate(R.layout.main, container, false); 
    v.addView(pager); 
    return v; 
} 

所有的工作,但是當我選擇了第二次的任何選項卡 - 我得到錯誤:

The specified child already has a parent. You must call removeView() on the child's parent first. 

我想,那個附加函數的錯誤,但是怎麼解決呢?

回答