我在这里缺少什么 - 在我的 activity 中实现选项卡?

What am I missing here - in implementing tabs in my activity?

我的 MainActivity class - 我想在其中实现固定选项卡

package com.nikhil.tabs;


import android.app.ActionBar;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager pager  = (ViewPager) findViewById(R.id.pager);

        pager.setAdapter(new TabsPagerAdapter(getSupportFragmentManager()));

        // How to add tabs here
       // What more is missing?

            } 

    //Do I need any listeners for tabs here?

}

下面是我的适配器 class 的代码。最初我使用 FragmentPagerAdapter 但我在 MainActivity 中遇到了 ActionBar 的一些问题。所以,现在我正在使用 FragmentStatePagerAdapter.

我的适配器Class

package com.nikhil.tabs;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    // Tab titles
    private String[] tabs = { "Top Rated", "Games"};

    @Override
    public Fragment getItem(int index) {

        switch (index) {
            case 0:
                // Top Rated fragment activity
                return new TopRatedFragment();
            case 1:
                // Games fragment activity
                return new GamesFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return tabs.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

}

如果我 运行 我的应用程序。我没有收到任何标签。请问少了什么?

如果我尝试添加

viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

在我的 class 中,NAVIGATION_MODE_TABS 被删除了,因为它已被弃用。尽管如此,如果我尝试 运行 该应用程序,它就会崩溃。

编辑:

我的Logcat:

03-07 23:03:02.350    2970-2970/com.nikhil.tabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nikhil.tabs, PID: 2970
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nikhil.tabs/com.nikhil.tabs.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
            at com.nikhil.tabs.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

使用 API 21 方法 setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) 已弃用。参考 this.

如果您喜欢 google 播放商店风格的标签,您可以使用 PagerSlidingTabStrip library

Google SlidingTabsBasic

的示例