-1

PS. i am new in android and java programming.

i have this problem in my application it crashes when i clicked on my intent button which handles tab activity (the deprecated one), there are no errors with some warnings but i can't seem to figure out what i am doing wrong.

here is the code.

InformationActivity.class

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class InformationActivity extends TabActivity
{
        /** Called when the activity is first created. */

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_information);

               // create the TabHost that will contain the Tabs
                TabHost tabHost = (TabHost)findViewById(R.id.tabhost);


               TabSpec tab1 = tabHost.newTabSpec("First Tab");
               TabSpec tab2 = tabHost.newTabSpec("Second Tab");


             // Set the Tab name and Activity
             // that will be opened when particular Tab will be selected
                tab1.setIndicator("Tab1");
                tab1.setContent(new Intent(this,OfflineTab.class));

                tab2.setIndicator("Tab2");
               tab2.setContent(new Intent(this,OnlineTab.class));

              /** Add the tabs  to the TabHost to display. */
              tabHost.addTab(tab1);
               tabHost.addTab(tab2);


        }
} 

OfflineTab.class

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class OfflineTab extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv=new TextView(this);
        tv.setTextSize(25);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText("Contains R.E.D. e - Kit Offline module");

        setContentView(tv);
    }
}

OnlineTab.class

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class OnlineTab extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView  tv=new TextView(this);
        tv.setTextSize(25);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText("This Is Tab2 Activity");

        setContentView(tv);
    }
 }

activity_information.xml

<?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">

    <LinearLayout
            android:id="@+id/LinearLayout01"
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

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

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

    </LinearLayout>

</TabHost>

1 Answers1

1

The log said: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

So just don't define a new id for tabhost:

activity_information.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:id="@+id/LinearLayout01"
    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>

onCraete() of InformationActivity

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_information);
           // create the TabHost that will contain the Tabs

            TabHost tabHost = getTabHost(); // <- get tabhost by this


           TabSpec tab1 = tabHost.newTabSpec("First Tab");
           TabSpec tab2 = tabHost.newTabSpec("Second Tab");


         // Set the Tab name and Activity
         // that will be opened when particular Tab will be selected
            tab1.setIndicator("Tab1");
            tab1.setContent(new Intent(this,OfflineTab.class));

            tab2.setIndicator("Tab2");
           tab2.setContent(new Intent(this,OnlineTab.class));

          /** Add the tabs  to the TabHost to display. */
          tabHost.addTab(tab1);
           tabHost.addTab(tab2);

}

And it worked! enter image description here

Xun Chenlong
  • 1,622
  • 2
  • 11
  • 14