0

我正在尝试从中添加文本TabViewTextView得到java.lang.IllegalStateException

活动代码:

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>
4

3 回答 3

0

正如错误所说video,并且text已经有了父母。由于您是从相同的检索它ViewGroup,它们被包裹在LinearLayout/ FrameLayout/RelativeLayout中,因此它们有一个父级。

于 2013-05-22T07:35:15.527 回答
0

您可以从代码本身添加文本视图,而不是使用布局

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

TextView video=new TextView(this);
video.setText()
video.setGravity()
video.setTextColor()
于 2013-05-22T07:54:05.220 回答
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();
于 2013-05-22T07:55:05.737 回答