2013-10-25 36 views
3

我有以下FragmentActivity:Android:无法加载标签。说孩子已经有一个父

public class TabsActivity extends FragmentActivity { 
private FragmentTabHost mTabHost; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs); 
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

    mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home", getResources().getDrawable(R.drawable.hometab)),HomeTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploretab)),ExploreTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Info").setIndicator("Info", getResources().getDrawable(R.drawable.infotab)),InfoTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Social").setIndicator("Social", getResources().getDrawable(R.drawable.socialtab)),SocialTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Contact").setIndicator("Contact", getResources().getDrawable(R.drawable.contacttab)),ContactTab.class, null); 

    TabWidget tabWidget = mTabHost.getTabWidget(); 
    tabWidget.setStripEnabled(false); 
    for(int i=0; i < tabWidget.getChildCount(); i++){ 
     tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg); 
     TextView tv = (TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title); 
     tv.setTextColor(this.getResources().getColor(R.color.white)); 
    } 

    mTabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      Log.d("PAUL",tabId); 
      if(tabId=="Home"){ 
       finish(); 
      } 
      TabWidget tw = mTabHost.getTabWidget(); 
      for(int i=0; i < tw.getChildCount(); i++){ 
       TextView tabText = (TextView)tw.getChildAt(i).findViewById(android.R.id.title); 
       if(tabText.getText()==tabId){ 
        tabText.setTextColor(TabsActivity.this.getResources().getColor(R.color.tcmgreen)); 
       } else { 
        tabText.setTextColor(TabsActivity.this.getResources().getColor(R.color.white)); 
       } 

      } 
     } 
    }); 

    Intent intent = getIntent(); 
    String tag = intent.getStringExtra("tab"); 
    Log.d("DEBUG",tag); 
    mTabHost.setCurrentTabByTag(tag); 

} 

} 

当用户点击“浏览”选项卡上,我得到以下错误:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.这是我ExploreTab类:

public class ExploreTab extends Fragment { 
private ArrayList<Level> levels; 
private TCMSQLiteHelper sqliteHelper; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    sqliteHelper = new TCMSQLiteHelper(this.getActivity()); 
    levels = sqliteHelper.getAllLevels(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    Log.d("DEBUG","Explore Created"); 
    View v = inflater.inflate(R.layout.explorelist, container); 

    return v; 
} 
} 

这对我来说没有任何意义,因为我没有看到哪里可能存在已加载视图的问题。我在这里错过了什么?

回答

2

onCreateView()中,您应该传入第三个参数false。第三个参数指定布局是否应将其自身附加到ViewGroup container

Fragments documentation

In this case, this is false because the system is already inserting the inflated layout into the container—passing true would create a redundant view group in the final layout.

因此,你的代码应该是:

View v = inflater.inflate(R.layout.explorelist, container, false); 
+0

这就是它!谢谢! – LoneWolfPR

相关问题