我有一个包含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>
答案 0 :(得分: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>
答案 1 :(得分:1)
我刚刚提交了一个Android工具bug report:
以下用户和我在使用时看到此错误 布局编辑器的图形布局选项卡,即使是最简单的和 正确的布局。
“无法创建标签内容,因为无法找到ID为-1的视图”
当我将另一个xml文件作为选项卡包含时,我在API20中看到了它。如果 我复制并粘贴包含文件的内容,错误不是 所示。
请参阅以下有关此错误的其他报告:
Could not create tab content because could not find view with id -1 XML error in Android Eclipse?
https://stackoverflow.com/a/4807779/403455(见关于raybritton的评论 建议答案)
https://groups.google.com/d/topic/android-developers/DRY4fsbwgDA/discussion