Android 对TabHost的意外强制转换:布局标记为线性布局

Android 对TabHost的意外强制转换:布局标记为线性布局,android,android-layout,android-tabhost,Android,Android Layout,Android Tabhost,我尝试创建一个tabHost,如下代码所示 TabHost tabs = (TabHost) findViewById(R.id.homeTabs); tabs.setup(); // Search TabHost.TabSpec tabSearch = tabs.newTabSpec("search"); tabSearch.setContent(R.id.tabSearch); tabSearch.setIndicator("Search")

我尝试创建一个tabHost,如下代码所示

    TabHost tabs = (TabHost) findViewById(R.id.homeTabs);
    tabs.setup();

    // Search
    TabHost.TabSpec tabSearch = tabs.newTabSpec("search");
    tabSearch.setContent(R.id.tabSearch);
    tabSearch.setIndicator("Search");
    tabs.addTab(tabSearch);

    // Notification
    TabHost.TabSpec tabNotification = tabs.newTabSpec("notification");
    tabNotification.setContent(R.id.tabNotification);
    tabNotification.setIndicator("Notification");
    tabs.addTab(tabNotification);
它的xml代码是

<TabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/homeTabs">

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

            <LinearLayout
                android:id="@+id/tabNotification"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tabSearch"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:layout_gravity="center">

            </LinearLayout>

        </FrameLayout>
    </LinearLayout>
</TabHost>
运行此应用程序时,它退出并显示此错误


致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.nisfansabith.policia/com.example.nisfansabith.policia.Home}:java.lang.ClassCastException:android.widget.LinearLayout

R.id.hometab
是xml中的
LinearLayout

TabHost tabs = (TabHost) findViewById(R.id.homeTabs);  
tabHost
是布局xml中
tabHost
组件的id。

更改以下行

TabHost tabs = (TabHost) findViewById(R.id.homeTabs);

ClassCastException:android.widget.LinearLayout

因为
homeTabs
是LinearLayout的id,但正在尝试强制转换
TabHost

使用
tabHost
而不是
hometab
从xml获取tabHost:

TabHost tabs = (TabHost) findViewById(R.id.tabHost);

TabHost tabs=(TabHost)findviewbyd(R.id.TabHost);更改此行TabHost的id是什么?@NisfanSabith:您正在XML中使用
R.id.TabHost
TabHost和LinearLayout的id是什么
TabHost tabs = (TabHost) findViewById(R.id.tabHost);
TabHost tabs = (TabHost) findViewById(R.id.tabHost);