Question

I want two tabs in my application which are in other activity which I access from my main activity from a button.

My Activity which will control the tabs has the code as below

    public class Myclass extends TabActivity


{


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myclass);


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

        TabSpec firstSpec = tabHost.newTabSpec("First");
        firstSpec.setIndicator("first Tab",null);
        Intent firstintent = new Intent(this,Tab1.class);
        firstSpec.setContent(firstintent);


        TabSpec secondSpec = tabHost.newTabSpec("Second");
        secondSpec.setIndicator("first Tab",null);
        Intent secondintent = new Intent(this,Tab2.class);
        secondSpec.setContent(secondintent);


        tabHost.addTab(firstSpec);
        tabHost.addTab(secondSpec);
    }
}





And the XML which has the tabHost content has the code

    <?xml version="1.0" encoding="utf-8"?>


    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabHost"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true">

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

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

            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">


            </FrameLayout>
        </LinearLayout>
    </TabHost>


So the app is compiling and loading up fine in my device and able to access other activites. But when I try to access the button which should display the activity above mentioned "Myclass" activity. The application exits and I get a screen displaying "Unfortunately,app has stopped".


I have entered the activities in the AndroidManifest.xml also.


The TabActivity shows a cancel mark on the word and says 'android.app.TabActivity' is deprecated.

What does this mean exactly?




Can any one suggest me what is going wrong with the program.













I changed the code as given in the  link http://developer.android.com/reference/android/app/TabActivity.html to the code below                                                   Code 

    public class MyClass extends FragmentActivity


    {

        private FragmentTabHost mTabHost;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.myclass);
            mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

            mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                   Tab1.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                    Tab2.class, null);
        }
    }

And the XML CODE to

<android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <TabWidget
                android:id="@android:id/tabs"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>

        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>

        <FrameLayout
                android:id="@+id/realtabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

But still no effect, the same problem of application exiting upon clicking of the button to start the activity Myclass continues.

Can anyone suggest a solution?

Was it helpful?

Solution

you should change

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

with

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

OTHER TIPS

try this:

make changes in your XML

Change Tab host id

android:id="@+id/tabHost" to android:id="@android:id/tabhost"

it should be like this :

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top