Search code examples
androidandroid-tabhost

TabHost without extending TabActivity


Android 2.3.3

I am just giving a try on how to implement TabHost without extending TabActivity. I get an IllegalArgumentException. Please have a look at the code below. I am not trying to do anything, just add 3 tabs to the TabHost.

Exception from Logcat :::

05-01 13:41:21.404: E/AndroidRuntime(362): FATAL EXCEPTION: main
05-01 13:41:21.404: E/AndroidRuntime(362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tabhostdemo/com.example.tabhostdemo.MainActivity}: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-01 13:41:21.404: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:507)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-01 13:41:21.404: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)
05-01 13:41:21.404: E/AndroidRuntime(362): Caused by: java.lang.IllegalArgumentException: you must specify a way to create the tab content
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.widget.TabHost.addTab(TabHost.java:202)
05-01 13:41:21.404: E/AndroidRuntime(362):  at com.example.tabhostdemo.MainActivity.onCreate(MainActivity.java:22)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-01 13:41:21.404: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-01 13:41:21.404: E/AndroidRuntime(362):  ... 11 more

MainActivity.Java:::

public class MainActivity extends Activity {

    private TabHost tabhost;
    private TabHost.TabSpec tabspec;

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

        tabhost = (TabHost) findViewById(R.id.tabhost);

        tabspec = tabhost.newTabSpec("SCAN");
        tabspec.setIndicator("Scan");
        tabhost.addTab(tabspec);

        tabspec = tabhost.newTabSpec("CREATE");
        tabspec.setIndicator("Create");
        tabhost.addTab(tabspec);

        tabspec = tabhost.newTabSpec("MORE");
        tabspec.setIndicator("More");
        tabhost.addTab(tabspec);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml:::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@+id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

Can anyone tell me what does it mean by this error - you must specify a way to create the tab content and how do i specify it?


Solution

  • With out extending the TabActivity, you can also implement the tab bar with the modifications in your xml file.

     <TabWidget
            android:id="@android:id/tabs"
    
     <FrameLayout
            android:id="@android:id/tabcontent"
    

    now intialize the tabhost and add tabs to your tabhost in this way.

      TabHost tab_host = (TabHost) findViewById(android.R.id.tabhost);
    
    
            tab_host.setup();
            // Set Tab Specification for Battery Tab
    
            TabSpec battery_tab_spec = tab_host.newTabSpec("assignments_tab");
    
            battery_tab_spec.setContent(R.id.assignments_tab);
            battery_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                    R.drawable.dashboard_assignments_student)));
            tab_host.addTab(battery_tab_spec);
    
            // Set Tab Specification for Network Tab
            TabSpec network_tab_spec = tab_host.newTabSpec("discussions_tab");
    
            network_tab_spec.setContent(R.id.discussions_tab);
            network_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                    R.drawable.dashboard_discuss)));
            tab_host.addTab(network_tab_spec);
    
            // Set Tab Specification for Device Tab
            TabSpec device_tab_spec = tab_host.newTabSpec("progress_tab");
    
            device_tab_spec.setContent(R.id.progress_tab);
            device_tab_spec.setIndicator(makeIndicator(getResources().getDrawable(
                    R.drawable.dashboard_track)));
            tab_host.addTab(device_tab_spec);
    

    Try this.