java.lang.ClassCastException: android.widget.RelativeLayout 无法转换为 android.widget.TabHost

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

我正在尝试将 Tabhost 添加到我的应用程序,但由于某些原因,当我 运行 该应用程序时,它给了我这个错误 java.lang.ClassCastException: android.widget.RelativeLayout无法转换为 android.widget.TabHost.

这里是完整的异常:

  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.access0(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.access0(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)

这是activity编辑的XML

   <?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代码

   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);

   }
 }

问题出在您的 xml 文件上,TabHost 的 ID 应该是 android:id="@android:id/tabhost":

因此请更改您的 xml 文件以实施此更改。

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

更新

您的 xml 文件应如下所示:

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