Bolandian Eran Bolandian Eran - 5 months ago 14
Android Question

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TabHost

I'm trying to add Tabhost to my app, but for some reason when i run the app it gives me this earror java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TabHost.

here is the full exception:

FATAL EXCEPTION: main
Process: com.world.bolandian.tests, PID: 4159
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.world.bolandian.tests/com.world.bolandian.tests.TabHostMain}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TabHost
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TabHost
at android.app.TabActivity.onContentChanged(TabActivity.java:128)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:391)
at android.app.Activity.setContentView(Activity.java:2188)
at com.world.bolandian.tests.TabHostMain.onCreate(TabHostMain.java:15)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
at android.app.ActivityThread.access$800(ActivityThread.java:148) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5272) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)


this is the XML of the activity EDITED

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.world.bolandian.tests.TabHostMain"
android:id="@+id/tabHost"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">

<LinearLayout
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/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>

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

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


Activity code

public class TabHostMain extends TabActivity {

TabHost tabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_host_main);


tabHost = getTabHost();

TabHost.TabSpec ts1 = tabHost.newTabSpec("main");
ts1.setIndicator("Main");
ts1.setContent(new Intent(this, Main.class));
tabHost.addTab(ts1);

TabHost.TabSpec ts2 = tabHost.newTabSpec("GPS");
ts2.setIndicator("GPS");
ts2.setContent(new Intent(this, GPS.class));
tabHost.addTab(ts2);


TabHost.TabSpec ts3 = tabHost.newTabSpec("Info");
ts3.setIndicator("Info");
ts3.setContent(new Intent(this, Info.class));
tabHost.addTab(ts3);

}
}

Answer

The problem is with your xml file, the id for the TabHost should be android:id="@android:id/tabhost":

So change your xml file to implement this change.

Btw TabActivity is now depricated and you should use it like this now.

UPDATE

Your xml file should look something like this :

<?xml version="1.0" encoding="utf-8"?>
<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.world.bolandian.tests.TabHostMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        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/Main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></LinearLayout>

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

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