Question

I am trying to create a tabbed application however, I'm finding problems when trying to run the application and the main class extends TabActivity. The Main Class code is the following:

package org.practical.newsupdate;

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

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

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

    new Intent().setClass(this, LocalNews.class);
    TabSpec localTab = host.newTabSpec("Local");
    localTab.setIndicator(getResources().getString(R.string.local),
    getResources().getDrawable(android.R.drawable.star_on));
    localTab.setContent(R.id.local_tab);
    host.addTab(localTab);

    new Intent().setClass(this, ForeignNews.class);
    TabSpec foreignTab = host.newTabSpec("Foreign");
    foreignTab.setIndicator(getResources().getString(R.string.foreign),
    getResources().getDrawable(android.R.drawable.star_on));
    foreignTab.setContent(R.id.foreign_tab);
    host.addTab(foreignTab);

    host.setCurrentTabByTag("Local");
}   
} 

The XML is the following:

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/newsimages" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/newsimages" />

    <TextView
        android:id="@+id/newspage_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/menu_newspage" />
</RelativeLayout>

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

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

            <LinearLayout
                android:id="@+id/local_tab"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

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

The log cat shows the following errors:

12-25 18:11:24.846: E/AndroidRuntime(569): FATAL EXCEPTION: main 12-25 18:11:24.846: E/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.practical.newsupdate/org.practical.newsupdate.NewsPage}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread.access$600(ActivityThread.java:122) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.os.Handler.dispatchMessage(Handler.java:99) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.os.Looper.loop(Looper.java:137) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread.main(ActivityThread.java:4340) 12-25 18:11:24.846: E/AndroidRuntime(569): at java.lang.reflect.Method.invokeNative(Native Method) 12-25 18:11:24.846: E/AndroidRuntime(569): at java.lang.reflect.Method.invoke(Method.java:511) 12-25 18:11:24.846: E/AndroidRuntime(569): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-25 18:11:24.846: E/AndroidRuntime(569): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-25 18:11:24.846: E/AndroidRuntime(569): at dalvik.system.NativeStart.main(Native Method) 12-25 18:11:24.846: E/AndroidRuntime(569): Caused by: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost' 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.TabActivity.onContentChanged(TabActivity.java:131) 12-25 18:11:24.846: E/AndroidRuntime(569): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.Activity.setContentView(Activity.java:1835) 12-25 18:11:24.846: E/AndroidRuntime(569): at org.practical.newsupdate.NewsPage.onCreate(NewsPage.java:13) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.Activity.performCreate(Activity.java:4465) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-25 18:11:24.846: E/AndroidRuntime(569): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 12-25 18:11:24.846: E/AndroidRuntime(569): ... 11 more

The error that is showing is that the application has suddenly stopped working. Can someone please help me?

Thanks in advance for any help

Was it helpful?

Solution

The following line is giving the error Java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

Is the package name same as Java package name or different. If yes please import full qualified R class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top