android如何使用FragmentTabHost的问题

我想我是人 2013-02-03 02:07:22
本人想做一个Tab切换的功能。miniSdkVersion=7,targetSdkVersion=16。使用TabActivity时提示已过时,要求使用Fragment等技术。百度了半天,知道了要android-support-v4.jar包支持,导入后,发现有FragmentTabHost等类,我到现在还没弄清楚FragmentActivity、Fragment、FragmentTabHost之间的关系,按照Google官方文档提供的FragmentTabHost的Sample代码,布局XML后,“Graphical Layout”视图提示:Exception raised during rendering: No tab known for tag null错误,运行后也崩溃。

请教高人,怎么解决这个问题?我希望我的应用能兼容2.1到4.x。
...全文
34905 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
风山晓 2015-04-13
  • 打赏
  • 举报
回复
这么做是可以的。
引用 12 楼 harkue 的回复:
貌似不用那么复杂

TabHost host = (TabHost) findViewById(R.id.tabhost);
        host.setup();

        TabHost.TabSpec homeSpec = host.newTabSpec("Home"); // This param will
                                                            // be used as tabId.
        homeSpec.setIndicator(null, // This param will diplay as title.
                getResources().getDrawable(R.drawable.home));
        homeSpec.setContent(R.id.tab1);
        host.addTab(homeSpec);

        TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
        garbageSpec.setIndicator(null, getResources().getDrawable(R.drawable.garbage));
        garbageSpec.setContent(R.id.tab2);
        host.addTab(garbageSpec);

        TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
        maybeSpec.setIndicator(null, getResources().getDrawable(R.drawable.help));
        maybeSpec.setContent(R.id.tab3);
        host.addTab(maybeSpec);

        host.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Toast toast = Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
                toast.show();
            }
        });

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="55dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="@string/image_name"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="1000dp"
                    android:layout_height="wrap_content"
                    android:text="@string/image_url"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <ListView
                android:id="@+id/list1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:footerDividersEnabled="true"
                android:headerDividersEnabled="true" >
            </ListView>
            <!-- <ImageView -->
            <!-- android:layout_width="wrap_content" -->
            <!-- android:layout_height="wrap_content" -->
            <!-- android:layout_gravity="center" -->
            <!-- android:src="@drawable/home" /> -->
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:contentDescription="@string/image_desc"
                android:src="@drawable/garbage" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:contentDescription="@string/image_desc"
                android:src="@drawable/help" />
        </LinearLayout>
    </FrameLayout>

</TabHost>
其中,注意 <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> 这里的id必须是这个名字~
小旺仔1111 2014-10-23
  • 打赏
  • 举报
回复
http://tech.ddvip.com/2013-07/1374008031199159.html,搜索android.support.v4.app.FragmentTabHost源码下载
lyc602 2014-08-14
  • 打赏
  • 举报
回复
13楼的可行
objs 2014-07-18
  • 打赏
  • 举报
回复
13楼布局解决了我的问题。
kmaxjuhyhiuih 2014-07-08
  • 打赏
  • 举报
回复
你们安卓的开发者真是悲催,一个tab还要写这么多代码。。太原始了。 试试QT/QML吧。java写界面简直欲哭无泪。
白书黑影 2014-05-09
  • 打赏
  • 举报
回复
Exception raised during rendering: No tab known for tag null Exception details are logged in Window > Show View > Error Log 解决了没 帖子都一年了 求教
Mr桃子 2014-04-09
  • 打赏
  • 举报
回复
楼主,这个问题有没有解决,我现在也在困扰,求指点!
harkue 2013-10-22
  • 打赏
  • 举报
回复
貌似不用那么复杂

TabHost host = (TabHost) findViewById(R.id.tabhost);
        host.setup();

        TabHost.TabSpec homeSpec = host.newTabSpec("Home"); // This param will
                                                            // be used as tabId.
        homeSpec.setIndicator(null, // This param will diplay as title.
                getResources().getDrawable(R.drawable.home));
        homeSpec.setContent(R.id.tab1);
        host.addTab(homeSpec);

        TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
        garbageSpec.setIndicator(null, getResources().getDrawable(R.drawable.garbage));
        garbageSpec.setContent(R.id.tab2);
        host.addTab(garbageSpec);

        TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
        maybeSpec.setIndicator(null, getResources().getDrawable(R.drawable.help));
        maybeSpec.setContent(R.id.tab3);
        host.addTab(maybeSpec);

        host.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                Toast toast = Toast.makeText(MainActivity.this, tabId, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
                toast.show();
            }
        });

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="55dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="@string/image_name"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="1000dp"
                    android:layout_height="wrap_content"
                    android:text="@string/image_url"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <ListView
                android:id="@+id/list1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:footerDividersEnabled="true"
                android:headerDividersEnabled="true" >
            </ListView>
            <!-- <ImageView -->
            <!-- android:layout_width="wrap_content" -->
            <!-- android:layout_height="wrap_content" -->
            <!-- android:layout_gravity="center" -->
            <!-- android:src="@drawable/home" /> -->
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:contentDescription="@string/image_desc"
                android:src="@drawable/garbage" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:contentDescription="@string/image_desc"
                android:src="@drawable/help" />
        </LinearLayout>
    </FrameLayout>

</TabHost>
其中,注意 <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> 这里的id必须是这个名字~
Carve_Time 2013-10-21
  • 打赏
  • 举报
回复
liutz1 2013-07-25
  • 打赏
  • 举报
回复
简单的页面切换就是view切换就可以了。
小草房 2013-05-15
  • 打赏
  • 举报
回复
引用 3 楼 liu149339750 的回复:
提示tabhost过时不是因为 FragmentTabHost ,而是因为ActionBar,ActionBar加上Fragment可以替代TabHost的!安卓4.0原生的联系人就是通过ActionBar+Fragment+ViewPager实现的。
tabhost并没有过时,过时的是TabActivity
binge 2013-04-24
  • 打赏
  • 举报
回复
我现在也在纠结这个问题,顶一下,期待高手出现
  • 打赏
  • 举报
回复
亲,先去弄清楚fragment先,再做tab
mmorss 2013-04-22
  • 打赏
  • 举报
回复
引用 2 楼 GLuoYer 的回复:
做了个demo,可以看一下: http://download.csdn.net/detail/gluoyer/5226669
不能运行
ruancaipu 2013-04-21
  • 打赏
  • 举报
回复
看别人的Dome怎么写的吧、、
liu149339750 2013-04-20
  • 打赏
  • 举报
回复
提示tabhost过时不是因为 FragmentTabHost ,而是因为ActionBar,ActionBar加上Fragment可以替代TabHost的!安卓4.0原生的联系人就是通过ActionBar+Fragment+ViewPager实现的。
花佟林雨月 2013-04-06
  • 打赏
  • 举报
回复
做了个demo,可以看一下: http://download.csdn.net/detail/gluoyer/5226669
花佟林雨月 2013-04-05
  • 打赏
  • 举报
回复
布局 中添加: <android.support.v4.app.FragmentTabHost android:id="@+id/prize_tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.app.FragmentTabHost> 再在fragment中 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.frg_prize, container, false); mTabHost = (FragmentTabHost) view.findViewById(R.id.prize_tabhost); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.prize_title); mTabHost.addTab(mTabHost.newTabSpec(“tag”).setIndicator(“NAME”), fragment, null); mTabHost.addTab(mTabHost.newTabSpec(“tag1”).setIndicator(“NAME1”), fragment, null); } R.id.prize_title是所在布局中的一项。 没发现异常呢? 或者你把代码贴一下,才能看错误。

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧