繁体   English   中英

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

这是活动EDITED的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>

活动代码

   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文件,对于该ID TabHost应该是android:id="@android:id/tabhost"

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

顺便说一句, TabActivity现在已被删除,您现在应该像这样使用它。

更新

您的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM