简体   繁体   English

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

[英]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 . 我正在尝试将Tabhost添加到我的应用程序中,但是由于某些原因,当我运行该应用程序时,它给了我这个java.lang.ClassCastException异常:android.widget.RelativeLayout无法转换为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 这是活动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>

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

   }
 }

The problem is with your xml file, the id for the TabHost should be android:id="@android:id/tabhost" : 问题是你的xml文件,对于该ID TabHost应该是android:id="@android:id/tabhost"

So change your xml file to implement this change. 因此,请更改您的xml文件以实现此更改。

Btw TabActivity is now depricated and you should use it like this now. 顺便说一句, TabActivity现在已被删除,您现在应该像这样使用它。

UPDATE 更新

Your xml file should look something like this : 您的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.

相关问题 Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText - Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.Button - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText 造成原因:java.lang.ClassCastException:android.widget.RelativeLayout无法转换为android.widget.TextView - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView Java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.FrameLayout - Java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 原因:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为com.slidingmenu.lib.SlidingMenu - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM