How can I enable actionbar tabs in a fragment?

情到浓时终转凉″ 提交于 2019-12-22 08:05:39

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/28901821/how-can-i-enable-actionbar-tabs-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!