2013-10-25 66 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

相關問題