Search code examples
androidandroid-tabhosttabview

How to add a tab to TabView


I am trying to build a TabView for my android application. This is my java code

    public class AllMain extends TabActivity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.all_main);

            // create the TabHost that will contain the Tabs
            TabHost tabHost = getTabHost();
            Resources res = getResources();

            TabSpec tab1 = tabHost.newTabSpec("First Tab").setIndicator("Tab1").setContent(new Intent().setClass(this, TabAll_all.class));
            TabSpec tab2 = tabHost.newTabSpec("Second Tab").setIndicator("Tab2").setContent(new Intent().setClass(this, TabAll_all.class));
            TabSpec tab3 = tabHost.newTabSpec("Third tab").setIndicator("Tab3").setContent(new Intent().setClass(this, TabAll_all.class));

            tabHost.addTab(tab3);
            tabHost.addTab(tab1);
            tabHost.addTab(tab2);

            tabHost.setCurrentTab(0);
        }

    }

But when I run the application the application closes showing "Unfortunately stopped" message.

I realized that commenting below lines won't give me the error

            tabHost.addTab(tab3);
            tabHost.addTab(tab1);
            tabHost.addTab(tab2);

Thanks in advance.


Solution

  • Adding a new activity to Manifest file did the trick

    Thanks