简体   繁体   中英

How can I enable actionbar tabs in a fragment?

I switched from Activity to Fragment and now I am getting the this error

java.lang.IllegalArgumentException: Tabs not supported in this configuration

from the following line

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

How can I make these tabs work in a fragment with base ActionBarActivity?

rest of the code

@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;
    }

stack trace

 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)

With the API 21 the method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) is deprecated. Refer this .

If you prefer to have a tabs like google play store style, u can use PagerSlidingTabStrip library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM