在Android上创建类似iPhone的标签栏

时间:2014-04-29 09:38:54

标签: android android-fragments tabs

非常新的android。我浏览了一些网站和官方网站,在登录屏幕后创建一个标签视图。但是当我创建标签时,它会显示no tab known for tag null。不知道如何创建4个标签。

这是我的代码。

<android.support.v4.app.FragmentTabHost
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">

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

和TabActivity.java有这个

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabWidget;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.NavUtils;

public class TabActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

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

        setContentView(R.layout.activity_tab);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("Home"),
                DeliverySitesActivity.class, null);
        TabWidget tabWidget = mTabHost.getTabWidget();
        if (tabWidget != null) {
            View tabWidgetChild = tabWidget.getChildAt(0);
            if (tabWidgetChild != null) {
                tabWidgetChild.setVisibility(View.GONE);
            }
        }
    }

}

我哪里错了?我需要屏幕底部的标签栏。这是我的LogCat

04-29 05:45:25.126: E/AndroidRuntime(781): FATAL EXCEPTION: main
04-29 05:45:25.126: E/AndroidRuntime(781): java.lang.ClassCastException: com.example.hello.DeliverySitesActivity cannot be cast to android.support.v4.app.Fragment
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.View.dispatchAttachedToWindow(View.java:12125)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2450)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2457)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2457)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2457)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.Choreographer.doFrame(Choreographer.java:532)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.os.Handler.handleCallback(Handler.java:730)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.os.Looper.loop(Looper.java:137)
04-29 05:45:25.126: E/AndroidRuntime(781):  at android.app.ActivityThread.main(ActivityThread.java:5103)
04-29 05:45:25.126: E/AndroidRuntime(781):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 05:45:25.126: E/AndroidRuntime(781):  at java.lang.reflect.Method.invoke(Method.java:525)
04-29 05:45:25.126: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-29 05:45:25.126: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-29 05:45:25.126: E/AndroidRuntime(781):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

FragmentTabHost期望Fragments作为选项卡的内容,但您使用Activity类作为选项卡的内容而不是Fragment类。

mTabHost.addTab(... DeliverySitesActivity.class, ...)