2011-05-14 115 views
1

错误/ AndroidRuntime(10985):引起:java.lang.IllegalStateException:指定的孩子已经有父母。您必须先调用孩子的父母上的removeView() 。Android选项卡指定的子项已具有父项。您必须先调用孩子父母上的removeView()

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

Codice(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; 
}    
+0

安置自己的完整的错误日志,并告诉我们这行中出现的异常(如MyCode.java:34是线MyCode.java 34) – kcoppock 2011-05-15 00:10:28

回答

1

您需要为每个选项卡添加不同的类,而不是三个选项卡中的相同。您可能需要为每个选项卡分别创建一个xml文件,而不是像现在一样为每个选项卡创建一个xml文件。

检查标签布局教程(here)以获取想法。每个.setContent(intent);指代不同的实例。

+0

而且,如果我提出了不同的意图,也创建了三个不同的XML文件,错误总是出现....这是日志错误 – Mimmo 2011-05-15 00:32:31

+0

05-15 02:32:53.681:错误/ AndroidRuntime(16799):致命例外:主 05-15 02:32:53.681:ERROR/AndroidRuntime(16799):java.lang.RuntimeException:无法启动活动ComponentInfo {it.android.infoconsumarori/it.android.infoconsumarori.dettaglio}:java.lang.IllegalStateException:指定的子项已经有一位家长。您必须先调用子对象的父对象的removeView()。 – Mimmo 2011-05-15 00:36:26

+0

您需要创建三个不同的**类**,而不是指向同一个类**的三个不同的**意图。 – Aleadam 2011-05-15 00:38:18

5

应该有以下方法。需要注意的是充气的第三个参数是false

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