NullPointerException:尝试调用虚方法' void android.support.v4.app.Fragment.setMenuVisibility(boolean)'在null对象引用上

时间:2017-05-11 04:51:34

标签: android-fragments android-viewpager

我正在尝试使用Tablayout实现viewpager。在这一切都很好的工作正常,但一个问题是,当我从另一个屏幕视图寻呼机回来时失去了他们与Tablayout的位置,当我滚动它的崩溃app.I找到许多解决方案,但所有这些都无法为此工作。这是我的代码适配器类: -

    public class Portfolio_Adapter extends FragmentStatePagerAdapter {

        int tabCount;

        public Portfolio_Adapter(FragmentManager fm,int tabCount) {
            super(fm);

            this.tabCount=tabCount;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position){

                case 0:
                    Websites_Fragment websites_fragment=new Websites_Fragment();
                    return websites_fragment;

                case 1:
                    Mobileapplication_fragment mobileapplication_fragment=new Mobileapplication_fragment();
                    return mobileapplication_fragment;
                case 2:
                    Graphics_Fragment graphics_fragment=new Graphics_Fragment();
                    return graphics_fragment;

                default:
                    return null;
            }

        }

        @Override
        public int getCount() {
            return tabCount;
        }
    }

片段类: -

public class Portfolio_fragment extends Fragment {

        TabLayout tabLayout;
        ViewPager viewPager;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            final View view = inflater.inflate(R.layout.portfolio_fragment, container, false);

            ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Portfolio");
            tabLayout = (TabLayout) view.findViewById(R.id.portfolio_tabLayout);
            tabLayout.setupWithViewPager(viewPager);

            tabLayout.addTab(tabLayout.newTab().setText("Websites"));
            tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
            tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


            viewPager = (ViewPager) view.findViewById(R.id.portfolio_pager);

            Portfolio_Adapter adapter = new Portfolio_Adapter(getChildFragmentManager(), tabLayout.getTabCount());

            viewPager.setAdapter(adapter);
            viewPager.setOffscreenPageLimit(3);

            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {

                    viewPager.setCurrentItem(tab.getPosition());

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

            ViewGroup slidingTabStrip = (ViewGroup) tabLayout.getChildAt(0);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                View tab = slidingTabStrip.getChildAt(i);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tab.getLayoutParams();
                layoutParams.weight = 1;
                tab.setLayoutParams(layoutParams);
            }

            return view;
        }

    }

1 个答案:

答案 0 :(得分:1)

您的代码是正确的,您只需要更改放置代码的位置

tabLayout.setupWithViewPager(viewPager);

tabLayout.addTab(tabLayout.newTab().setText("Websites"));
tabLayout.addTab(tabLayout.newTab().setText("Mobile Application"));
tabLayout.addTab(tabLayout.newTab().setText("Graphics"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

在viewPager中的setadapter

之后
 viewPager.setAdapter(adapter);
 viewPager.setOffscreenPageLimit(3);