Android 对TabHost的意外强制转换:布局标记为线性布局
我尝试创建一个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 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);