TabHost運行時報錯的問題

問題1. 運行Activity的時候出現Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

添加Layout的時候,xml跟元素選擇TabHost, 但是ADT沒有添加id屬性, 運行的 時候,會提示Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'錯誤, 需要添加android:id="@android:id/tabhost", 這樣就 可以了。


問題2. 運行Activity的時候出現Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabcontent'

解決方法: 修改FrameLayout添加id屬性, ADT自動生成的xml文件中Id是 android:id="@+id/FrameLayout01 ", 需要修改成下面的格式 android:id="@android:id/tabcontent ",這個估計會困擾一大批初學者,誰會想到 會修改這個地方,看到錯誤很容易修改成tabcontent,但是前綴不容易想到。 而且 在ADT可視化編輯這個文件的時候, 界面上顯示NullPointerException,這個是 ADT的一個BUG。

修改後的xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">


<LinearLayout 

android:id="@+id/LinearLayout01" 

android:layout_height="fill_parent" 

android:layout_width="fill_parent" 

android:orientation="vertical">

<TabWidget 

android:id="@android:id/tabs" 

android:layout_height="wrap_content" 

android:layout_width="fill_parent">
</TabWidget>


<FrameLayout 

android:id="@android:id/tabcontent" 

android:layout_width="fill_parent" 

android:layout_height="fill_parent">
</FrameLayout>

 
</LinearLayout>
</TabHost>

注意: 如果用TabHost的話, 上面標紅的三處必須是一樣, 這個是Google的約定。 而且一個工程中只能有一個TabHost。

發佈了36 篇原創文章 · 獲贊 4 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章