1
    class PagesAdapter extends FragmentPagerAdapter 
     {
    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        //Return total pages, here one for each data item
        return pageData.length;}

    @Override
    public Fragment getItem(int position) {
        return null;
    }

    //Create the given page (indicated by position)
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
   View page = null;
   page = (View) inflater.inflate(R.layout.page,null,false);
   ((TextView)page.findViewById (R.id.textMessage)).setText(pageData[position]);
        //Add the page to the front of the queue
        ((ViewPager) container).addView(page, 0);
        return page;
    }

Error:

1-16 18:41:34.354 24761-24761/? E/AndroidRuntime: FATAL EXCEPTION: main
11-16 18:41:34.354 24761-24761/? E/Android 
Runtimejava.lang.NullPointerException
11-16 18:41:34.354 24761-24761/? E/AndroidRuntime: atOnlytext $ MyPagesAdapter.instantiateItem
11-16 18:41:34.354 24761-24761/? E/AndroidRuntime:at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
11-16 18:41:34.354 24761-24761/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1020)
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Sachin M Jain
  • 75
  • 1
  • 9

1 Answers1

-1
Maybe :


   @Override
    public Object instantiateItem(ViewGroup container, int position) {
   View page = null;
   page = (View) inflater.inflate(R.layout.page,container,false);
   ((TextView)page.findViewById (R.id.textMessage)).setText(pageData[position]);
        //Add the page to the front of the queue
        ((ViewPager) container).addView(page, position);
        return page;
    }
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Maybe [programming by permutation](https://en.wikipedia.org/wiki/Programming_by_permutation) ... please explain how changing `addView(page, 0)` to `addView(page, position)` would help with this NPE? – Selvin Nov 16 '15 at 13:36
  • No .it inflater.inflate(R.layout.page,null ,false) to inflater.inflate(R.layout.page,container,false) – tiny sunlight Nov 16 '15 at 13:41
  • doesn't matter ... this parameter of `inflate` **can** be null ... while it is better to not use null there it will not cause NPE ... – Selvin Nov 16 '15 at 13:42
  • yeah ... just take a look at LayoutInflater docs or source ... the key part of docs is **Optional** near description of `root` parameter ... – Selvin Nov 16 '15 at 13:48