3

我有以下片段活动:

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

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

4

1 回答 1

2

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

片段文档

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

因此,您的代码应该是:

View v = inflater.inflate(R.layout.explorelist, container, false);
于 2013-10-25T19:36:34.520 回答