4
votes

I have an XML file which contains a TabHost. I created it directly by right clicking the layout map -> new -> Android XML -> TabHost. The code seems to be ok but whenever I go to graphical layout in Eclipse, it gives me the error (see the title). I'm using Android 2.2. This is my XML code:

<?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>
2
I'm having this too in ADT20 in my layout. Yours doesn't give me the error. What version are you using?Jeff Axelrod
I have the newest (I think 20.0.3) but I managed to solve this by cleaning the project and restarting eclipse. It worked fine after that, apparently some layout files weren't loaded correctly or something.ZimZim

2 Answers

4
votes

The solution for me was giving id to each tab's view. In the following example there was a need to give id to each textView (like 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
votes

I just filed an Android tools bug report on this:

The following users and I have seen this error crop up when using the Graphical Layout tab of the layout editor, even on the simplest and correct layouts.

"Could not create tab content because could not find view with id -1"

I'm seeing it in API20 when I including another xml file as a tab. If I copy and paste the contents of the include file, the error is not shown.

See the following other reports of this error:

Could not create tab content because could not find view with id -1 XML error in Android Eclipse?

https://stackoverflow.com/a/4807779/403455 (See comment on raybritton's proposed answer)

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