0

I try to create swipe tabs view. But got an error within my code.

Here is my code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    mAdapter = new MyPagerAdapter(getSupportFragmentManager());

    mToolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(mToolbar);

    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mTabLayout.setTabsFromPagerAdapter(mAdapter);
    mTabLayout.setupWithViewPager(mPager);
    mPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}

public static class MyFragment extends Fragment {
    public static final String ARG_PAGE = "arg_page";

    public MyFragment() {

    }

    public static MyFragment newInstance(int pageNumber) {
        MyFragment myFragment = new MyFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ARG_PAGE, pageNumber);
        myFragment.setArguments(arguments);
        return myFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        int pageNumber = arguments.getInt(ARG_PAGE);
        TextView myText = new TextView(getActivity());
        myText.setText("Hello, " + pageNumber);
        myText.setGravity(Gravity.CENTER);
        return myText;

    }
}

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public Fragment getItem(int position) {
        ThirdActivity.MyFragment myFragment = new ThirdActivity.MyFragment();
        return myFragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "Tab" + position;
    }
}

I can't find how to fix it. the error is on line 85:

 int pageNumber = arguments.getInt(ARG_PAGE);

Any suggestion? What should i add so it can work properly.

I've read this link

What is a NullPointerException, and how do I fix it?

but can't get it why my code can return null. when i read the code it should return the value.

 arguments.putInt(ARG_PAGE, pageNumber);

and i take the arg_page number. so the value should be there.

Thanks

Community
  • 1
  • 1
ssuhat
  • 7,387
  • 18
  • 61
  • 116
  • nope. when i read my code it should return the value. arguments.putInt(ARG_PAGE, pageNumber); at this one. ARG_PAGE should get the value of pageNumber. – ssuhat Jul 28 '15 at 10:59
  • hehehe, you never use this code ... you still using the constructor ... I just love when peoples do not understand their own code ... – Selvin Jul 28 '15 at 11:03
  • @sstarlight You create a `MyFragment.newInstance` but never use it. Change the code in `MyPagerAdapter.getItem` to `ThirdActivity.MyFragment myFragment = ThirdActivity.MyFragment.newInstance(position)`. Hope this helps – justHooman Jul 28 '15 at 11:03
  • @Selvin yes. still new to android development. :) and still try to understand every single code. – ssuhat Jul 28 '15 at 11:10

0 Answers0