无法在ViewPager中设置FragmentStatePagerAdapter

时间:2016-10-03 06:56:43

标签: android nullpointerexception

我想通过方法

在ViewPager中设置FragmentStatePagerAdapter子类的对象
viewPager.setAdapter(myPagerAdapter);

但我得到的错误如下

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
                                                       at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:120)
                                                       at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1006)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1154)
                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)

请帮我解决这个问题。

代码段

    public class MainActivity extends AppCompatActivity {

        Toolbar toolbar;
        TabLayout tabLayout;
        ViewPager viewPager;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar)findViewById(R.id.toolbar);
            toolbar.setTitle("My App");
            toolbar.setSubtitle("this is my test");
            tabLayout = (TabLayout)findViewById(R.id.tablayout);
            tabLayout.addTab(tabLayout.newTab().setText("Left"));
            tabLayout.addTab(tabLayout.newTab().setText("Cent"));
            tabLayout.addTab(tabLayout.newTab().setText("Right"));

            viewPager = (ViewPager)findViewById(R.id.viewpage);

            MyPagerAdapter myPagerAdapter = new MyPagerAdapter(
                    getSupportFragmentManager(),
                    tabLayout.getTabCount()
            );
            viewPager.setAdapter(myPagerAdapter);
            //viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        }
    }

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    int tabCount;
    public MyPagerAdapter(FragmentManager fm, int tabCount) {
        super(fm);
        this.tabCount = tabCount;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 1:
                Fragment1 fragment1 = new Fragment1();
                return fragment1;
            case 2:
                Fragment2 fragment2 = new Fragment2();
                return fragment2;
            case 3:
                Fragment3 fragment3 = new Fragment3();
                return fragment3;
            default:
                return null;
        }
    }

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

2 个答案:

答案 0 :(得分:2)

虽然您应该使用tabLayout.setupWithViewPager(viewPager);

这是问题

switch (position){     
    // case 1, 2, 3...      
    default:
        return null; // Here

您是否忘记了列表在0位开始索引?您已返回null片段

答案 1 :(得分:0)

请尝试以下操作。

<强> MyPagerAdapter.java

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    private Context mContext;

    private List<Fragment> mFragments = new ArrayList<>();

    public FragmentPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.mContext = context;
    }

    public void addFragment(Fragment fragment) {
        mFragments.add(fragment);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
 }

在您的MainActivity中,

private void setUpViewPager(ViewPager viewPager) {
        adapterViewPager = new MyPagerAdapter(getApplicationContext(), getSupportFragmentManager());
        adapterViewPager.addFragment(new AFragment());
        viewPager.setAdapter(adapterViewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

private void setUpTabLayout(TabLayout tabLayout) {
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            //set your tab text, image etc here.
        }
        tabLayout.requestFocus();
    }

然后在onCreate()中,只需相应地使用这些方法即可。 setUpViewPager()跟随setUpTabLayout()