如何在片段中启用操作栏选项卡?

时间:2015-03-06 15:17:45

标签: java android android-fragments android-viewpager android-tabs

我从Activity切换到Fragment,现在我收到了这个错误

  

java.lang.IllegalArgumentException:此处不支持选项卡   构造

来自以下一行

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

如何使这些标签在基础ActionBarActivity的片段中工作?

其余代码

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        act = (ActionBarActivity )getActivity();

        final ActionBar actionBar = act.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mViewPager = (ViewPager) inflater.inflate(R.layout.activity_saved, container, false);

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getFragmentManager());

        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(SavedActivity.this));
        }


        return mViewPager;
    }

堆栈跟踪

 java.lang.IllegalArgumentException: Tabs not supported in this configuration
at android.support.v7.internal.app.ToolbarActionBar.setNavigationMode(ToolbarActionBar.java:325)
at my.package.myFragment.onCreateView(myFragment.java:48)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

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

如果您希望使用Google Play商店风格的标签,则可以使用PagerSlidingTabStrip library