2

I am a newbie in Android app development, I tried to create a nested tabs. That's I firstly create three tabs, then I define the content of the first tab to be another tabs activity. What I did is illustrated below:

I defined main tabs activity( with the content of the first tab to be another tabs activity):

res/layout/main.xml :

<?xml version="1.0" encoding="utf-8"?>
<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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

my main tabs activity class:

public class MyTest extends TabActivity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTabHost = getTabHost();

        //the first tab's content is another tabs activity        
        Intent tabs2=new Intent(this, SecondTabsActivity.class);
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(tabs2));

        //other tabs' content are just TextView
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));

        mTabHost.setCurrentTab(0);
    }
}

As you saw above, I would like the content of the first tab to be another tab activity, so I firstly defined a intend of the 2nd level tabs,then set content of first tab to be that intent.

The 2nd level tabs layout:

res/layout/level2tabs.xml:

<?xml version="1.0" encoding="utf-8"?>
<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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView 
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a tab" />
            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

the corresponding class for level 2 tabs:

public class SecondTabsActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level2tabs);

        TabHost mTabHost = getTabHost();

        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.layout.nestedtabs));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.textview3));

        mTabHost.setCurrentTab(0);
    }

But when I run the application, it stopped unexpectedly. I do not know where am I wrong with my nested tabs??? }

karllindmark
  • 6,031
  • 1
  • 26
  • 41
Leem
  • 17,220
  • 36
  • 109
  • 159
  • show your logcat error . what error you are exactly getting ?? – Sujit Jun 29 '11 at 07:40
  • @Sujit, I am a newbie, I do not know how to show logic error? From eclipse console, I did not get any error message, I only get error message from emulator which pop up an alert with message "The application has stopped unexpectedly. Please try again" – Leem Jun 29 '11 at 07:45
  • 1
    see this http://developer.android.com/guide/developing/debugging/ddms.html – Sujit Jun 29 '11 at 08:05
  • @Sujit, thank you. Then I got the error log that SecondTabsActivity can not be found. It complains that I did not define this activity. Do I need to declare this activity in some configuration files?? I only create the Java class... – Leem Jun 29 '11 at 08:22

1 Answers1

2

use <activity android:name=".SecondTabsActivity"/> in your AndroidManifest.xml file inside application tag.

Sujit
  • 10,512
  • 9
  • 40
  • 45