如何将选项卡添加到滑动选项卡的 ActionBar?

How to add Tabs to ActionBar for Swipe Tabs?

我想用 ActionBar 创建两个滑动标签。

这是我的代码:

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.rastari.salar.mymetarialbank.R;

/**
 * Created by Salar on 9/5/2015.
 */
public class AccountsActivityChart extends FragmentActivity implements ActionBar.TabListener {

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

        actionBar=getActionBar();
        actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);

        //Add the Action Bar Tabs
        actionBar.addTab(actionBar.newTab().setText("Accounts Activity"+"Chart Activity").setTabListener(this));

    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }
}

当我 运行 应用程序出现此错误时:

> 05-11 06:09:28.013    2371-2371/? E/AndroidRuntime﹕ FATAL EXCEPTION:
> main
>     Process: com.rastari.salar.mymetarialbank, PID: 2371
>     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rastari.salar.mymetarialbank/com.rastari.salar.mymetarialbank.activity.AccountsActivityChart}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.app.ActionBar.setNavigationMode(int)' on a null object
> reference
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
>             at android.app.ActivityThread.access0(ActivityThread.java:151)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:135)
>             at android.app.ActivityThread.main(ActivityThread.java:5254)
>             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:903)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
>      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on
> a null object reference
>             at com.rastari.salar.mymetarialbank.activity.AccountsActivityChart.onCreate(AccountsActivityChart.java:21)
>             at android.app.Activity.performCreate(Activity.java:5990)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
>             at android.app.ActivityThread.access0(ActivityThread.java:151)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:135)
>             at android.app.ActivityThread.main(ActivityThread.java:5254)
>             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:903)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 05-11
> 06:09:30.037      444-504/? E/InputDispatcher﹕ channel '155e1fbc
> com.rastari.salar.mymetarialbank/com.rastari.salar.mymetarialbank.activity.MainActivity
> (server)' ~ Channel is unrecoverably broken and will be disposed!
> 05-11 06:09:30.037      444-504/? E/InputDispatcher﹕ channel '2f2170b7
> com.rastari.salar.mymetarialbank/com.rastari.salar.mymetarialbank.activity.Login
> (server)' ~ Channel is unrecoverably broken and will be disposed!

真不知道该怎么办! 你们能指导我吗? 非常感谢。

最新代码:

import android.support.v7.app.ActionBar
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.rastari.salar.mymetarialbank.R;

public class AccountsActivityChart extends ActionBarActivity implements ActionBar.TabListener {

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

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);

        //Add the Action Bar Tabs
        actionBar.addTab(actionBar.newTab().setText("Accounts Activity"+"Chart Activity").setTabListener(this));
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }
}

首先,您正在创建一个 FragmentActivity 并期望看到一个不会发生的 ActionBar

替换

AccountsActivityChart extends FragmentActivity

AccountsActivityChart extends ActionBarActivity

其次,您需要调用

actionBar = getSupportActionBar();

而不是

actionBar = getActionBar();

因为 ActionBarActivityappcompat-v7 库的一部分。这就是当您调用 actionBar.setNavigationMode() 时您的应用程序崩溃的原因,因为 getActionBar() returns nullFragmentActivity.

一起使用时

完成此操作后,您应该可以使用

actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);

成功并在 ActionBar 下看到可滑动的标签。

编辑:

替换

import android.app.ActionBar

import android.support.v7.app.ActionBar