Android Tab Views - could not create tab content because could not find view with id

只谈情不闲聊 提交于 2019-12-01 21:36:37

Your xml should have something like ,

  <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"
          >

    <FrameLayout  
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>   

    <TextView android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>

    <TextView android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="test"/>

    </FrameLayout>
    </TabHost>

examples: http://www.androidhive.info/2011/08/android-tab-layout-tutorial/ http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android

you should have TabHost and TabWidget in your xml layout, take a look at the Tab Layout example.

you need add tabwidget in the xml.... Checkout, this is how to use tab widget :

tabscroll.xml

<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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>


    public class Tabs5 extends TabActivity implements TabHost.TabContentFactory {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tabs_scroll);

        final TabHost tabHost = getTabHost();

        for (int i=1; i <= 30; i++) {
            String name = "Tab " + i;
            tabHost.addTab(tabHost.newTabSpec(name)
                    .setIndicator(name)
                    .setContent(this));
        }
    }

    /** {@inheritDoc} */
    public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }
}

Courtesy : http://developer.android.com/resources/samples/ApiDemos/res/layout/tabs_scroll.html AND http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/Tabs5.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!