java.lang.IllegalStateException ..... at tabhost.add(tabspec);

时间:2012-02-23 08:53:01

标签: android

嗨guyzz我有TabActivity,如下面的简单代码

public class TabhostActivity extends Activity{
    TabHost host;

    public class TabhostActivity extends Activity{
    TabHost host;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tablayout);

        host = (TabHost) findViewById(R.id.mytabhost);

        host.setup();
        Intent intent = new Intent(getApplicationContext(), RatingBarActivity.class); 
        TabHost.TabSpec spec = host.newTabSpec("positions").setIndicator("Positions",getResources().getDrawable(R.drawable.add)).setContent(intent);

        host.addTab(spec);
        host.setCurrentTab(0);

    }

并有一个简单的标签布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/mytabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:padding="5dp">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:padding="5dp">

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" 
                android:background="#FF0000">

            </FrameLayout>

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TabWidget
                    android:layout_marginLeft="5dp"
                    android:id="@android:id/tabs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:tabStripEnabled="true" >
                </TabWidget>
            </HorizontalScrollView>
        </LinearLayout>
    </TabHost>

</LinearLayout>

那么请帮助我代码出错的地方?

我有例外
  

02-23 14:09:47.920:E / AndroidRuntime(1000):引起:java.lang.IllegalStateException:您是否忘记调用'public void setup(LocalActivityManager activityGroup)'?

1 个答案:

答案 0 :(得分:21)

您正在扩展Activity课程,请尝试扩展TabActivity课程

如果要使用Activity类,请使用以下命令:

host = (TabHost) findViewById(R.id.tabhost); //here tabHost will be your Tabhost
    LocalActivityManager mLocalActivityManager = new LocalActivityManager(mActivity, false);
    mLocalActivityManager.dispatchCreate(state); // state will be bundle your activity state which you get in onCreate
    tabHost.setup(mLocalActivityManager);