ERROR / AndroidRuntime(10985):由以下原因引起:java.lang.IllegalStateException:指定的子代已经有一个父代。必须首先在该子代的父代上调用removeView()。


这是我用于可视化标签的XML tabs_dettaglio.xml

编码(XML):[Seleziona]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:id="@+id/relativeimg"
    >
    <RelativeLayout
        android:id="@+id/relativeimg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:id="@+id/imageView3"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip">
        </ImageView>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/relativeimg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/imageView2"
            android:layout_marginLeft="220dip">
        </ImageView>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/relativeimg3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/imageView1"
            android:layout_marginLeft="97dip">
        </ImageView>
    </RelativeLayout>
</RelativeLayout>


这是java代码:

private void setTabs() {
    tabHost = getTabHost();
    tabHost.addTab(tabHost.newTabSpec("1").setContent(new Intent(this,d.class)).setIndicator(addImg(1)));
    tabHost.addTab(tabHost.newTabSpec("2").setContent(new Intent(this,d.class)).setIndicator(addImg(2)));
    tabHost.addTab(tabHost.newTabSpec("3").setContent(new Intent(this,d.class)).setIndicator(addImg(3)));
}

private View addImg(int i) {
    int cc;
    int cc2;
    if(i==1) {
        cc = R.id.imageView3;
        cc2 = R.drawable.menu_oggi;
    } else if(i==2){
        cc = R.id.imageView2;
        cc2 = R.drawable.menu_ieri;
    } else {
        cc = R.id.imageView1;
        cc2 = R.drawable.menu_domani;
    }
    View tabIndicator = LayoutInflater.from(getBaseContext()).inflate(R.layout.tabs_dettaglio,null);
    ImageView icon = (ImageView) tabIndicator.findViewById(cc);
    icon.setBackgroundResource(cc2);
    return icon;
}

最佳答案

应该有以下方法。注意,inflate的第三个参数是false

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.pane0, container, false);
}

10-08 03:01