4

我有一个包含 TabHost 的 XML 文件。我通过右键单击布局图 -> 新建 -> Android XML -> TabHost 直接创建它。代码似乎没问题,但每当我在 Eclipse 中进行图形布局时,它都会给我错误(见标题)。我使用的是安卓 2.2。这是我的 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="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        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/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>
4

2 回答 2

4

我的解决方案是为每个选项卡的视图提供 id。在以下示例中,需要为每个 textView 提供 id(如 android:id="@+id/tab1" ...)

<?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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:tag="tab1"
            android:text="@string/tab1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            />
        <TextView
            android:tag="tab2"
            android:text="@string/tab2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            />
        <TextView
            android:tag="tab3"
            android:text="@string/tab3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            />
        <TextView
            android:tag="tab4"
            android:text="@string/tab4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            />
        <TextView
            android:tag="tab5"
            android:text="@string/tab5"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textSize="12sp"
            />

    </TabWidget>

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

        <TextView
            android:id="@+id/tab1"
            android:text="Hallo3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/tab2"
            android:text="Hallo2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/tab3"
            android:text="Hallo3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/tab4"
            android:text="Hallo3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
        <TextView
            android:id="@+id/tab5"
            android:text="Hallo3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </FrameLayout>
</LinearLayout>
</TabHost>
于 2013-03-14T11:08:10.137 回答
1

我刚刚提交了一份 Android 工具错误报告

以下用户和我在使用布局编辑器的图形布局选项卡时看到了此错误,即使在最简单和正确的布局上也是如此。

“无法创建标签内容,因为找不到 ID -1 的视图”

当我将另一个 xml 文件包含为选项卡时,我在 API20 中看到了它。如果我复制并粘贴包含文件的内容,则不会显示错误。

请参阅以下有关此错误的其他报告:

无法创建选项卡内容,因为在 Android Eclipse 中找不到 ID -1 XML 错误的视图?

https://stackoverflow.com/a/4807779/403455(请参阅对raybritton提出的答案的评论)

https://groups.google.com/d/topic/android-developers/DRY4fsbwgDA/discussion

于 2012-09-19T16:14:23.740 回答