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

标签 android xml android-layout tabs android-tabhost

我有一个包含 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>

最佳答案

我的解决方案是为每个选项卡的 View 指定 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>

关于android - 无法创建选项卡内容,因为在 Android Eclipse 中找不到 ID 为 -1 的 View XML 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338023/

相关文章:

java.lang.NullPointerException 无法找出导致错误的原因

"any one or more of these elements but must be at least one"的 XML 架构构造

android - 将字节数组 (Byte[]) 转换为视频 (Android)

java - 一次写入一个值

android - 发生意外错误 : ant jar >/dev/null exited with 127 in phonegap

php - 如何从 MediaWiki API 获取主体图像?

java - 如何设置这个 View ?

android - 我如何在 TextView 中使用 Roboto Black 字体

android - 扩展 FrameLayout, subview 将不再显示

android - 将数据发送到父 Activity onBackPressed