在Android Eclipse中,由于找不到ID为-1的视图,无法创建选项卡内容,导致XML错误。

4

我有一个包含TabHost的XML文件。我通过右键单击布局图并选择“新建” -> “Android XML” -> “TabHost”直接创建了它。代码似乎没问题,但每当我进入Eclipse中的图形化布局时,它就会给我错误提示(见标题)。我正在使用Android 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>

我在我的布局中也遇到了ADT20的这个问题。你的没有给我错误。你使用的是哪个版本? - Jeff Axelrod
我有最新的版本(我认为是20.0.3),但我通过清理项目并重新启动Eclipse来解决了这个问题。之后它正常工作了,显然一些布局文件没有正确加载或者出了什么问题。 - ZimZim
2个回答

4
我的解决方案是给每个选项卡视图都分配一个ID。在下面的示例中,需要为每个TextView(例如android:id =“@ + id / tab1” ...)分配一个ID。
<?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>

1

我刚刚在这个问题上提交了一个Android工具错误报告

以下用户和我在使用布局编辑器的图形布局选项卡时遇到了此错误,即使是最简单和正确的布局也会出现此错误。
“无法创建选项卡内容,因为找不到ID为-1的视图”
当我将另一个xml文件作为选项卡包含时,我在API20中看到它。如果我复制并粘贴包含文件的内容,则不会显示错误。
请参见以下其他关于此错误的报告: Could not create tab content because could not find view with id -1 XML error in Android Eclipse? https://dev59.com/GlPTa4cB1Zd3GeqPgy7w#4807779(请参见raybritton提出的答案的评论) https://groups.google.com/d/topic/android-developers/DRY4fsbwgDA/discussion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接