2013-05-22 85 views
0

我试图从TextViewTabView添加文本,但得到java.lang.IllegalStateExceptionjava.lang.IllegalStateException:指定的子项已具有父项。你必须对孩子的父母先调用removeView()的Android

活动代码:

public class Testimonial extends TabActivity { 
private TabHost tabHost; 
TextView video = null; 
TextView text = null; 
private void setupTabHost() { 
    tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.testimonial); 
    setupTabHost(); 

    View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.testimonial_tab_row, null); 
    video = (TextView)view.findViewById(R.id.tabVideo); 
    text = (TextView)view.findViewById(R.id.tabText); 

    tabHost = getTabHost(); 
    TabSpec videoSpec = tabHost.newTabSpec("Video"); 
    videoSpec.setIndicator(video); 
    Intent videoIntent = new Intent(this,TestimonialVideo.class); 
    videoSpec.setContent(videoIntent); 
    TabSpec textSpec = tabHost.newTabSpec("Text"); 
    textSpec.setIndicator(text); 
    Intent textIntent = new Intent(this, TestimonialText.class); 
    textSpec.setContent(textIntent); 

    tabHost.addTab(videoSpec); 
    tabHost.addTab(textSpec); 
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabdrawable); 
    } 
    tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater); 
} 
} 

这里是我的testimonial.xml代码

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_above="@android:id/tabs"/> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/tabdrawable" 
     android:gravity="center_vertical|center_horizontal" /> 

</RelativeLayout> 

</TabHost> 

这里是testimonial_tab_row.xml代码

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabsLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dip" 
android:gravity="center" 
android:orientation="vertical"> 
<TextView android:id="@+id/tabVideo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Video" 
    android:gravity="center" 
    android:textSize="22sp" 
    android:textColor="#fff" /> 

<TextView android:id="@+id/tabText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Text" 
    android:gravity="center" 
    android:textSize="22sp" 
    android:textColor="#fff" /> 
</LinearLayout> 
+0

你也应该贴上你的XML布局文件中添加的TextView。 – Prateek

回答

0

而是采用了布局,可以从代码本身

TextView text=new TextView(this); 
text.setText() 
text.setGravity() 
text.setTextColor() 

TextView video=new TextView(this); 
video.setText() 
video.setGravity() 
video.setTextColor() 
0

正如erro所说的videotext已经是父母。由于您从同一个ViewGroup中恢复它们,因此它们被包装在一个LinearLayout/FrameLayout/RelativeLayout中,所以它们有一个父亲。

0
try this 
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabdrawable); 
    } 
    tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater); 
    tabHost.removeAllViews(); 
相关问题