問題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 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 實現Tab標籤的居底主要是通過設定屬性 android:layout_weight="1" --> <!-- 還要注意FrameLayout標籤的位置,要寫在TabWidget標籤的前面 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </LinearLayout></TabHost>