Android:无法加载标签。说孩子已经有了父母

时间:2013-10-25 19:25:03

标签: android android-fragments android-fragmentactivity

我有以下FragmentActivity:

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

这对我没有任何意义,因为我没有看到可能存在已加载视图的问题。我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

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

来自Fragments documentation

  

在这种情况下,这是错误的,因为系统已经将膨胀的布局插入到容器中 - 传递true将在最终布局中创建冗余视图组。

因此你的代码应该是:

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