Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:52:45+00:00 2026-06-16T22:52:45+00:00

I am using viewPager and would like to add fragments to it: public class

  • 0

I am using viewPager and would like to add fragments to it:

public class MyFragment extends Fragment {
       public View onCreateView(LayoutInflater inflater, ViewGroup contner,
       Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.tab_frag2_layout, container, false);

        return view;
    }
    public static Fragment newInstance() {
        // TODO Auto-generated method stub
        MyFragment mfg = new MyFragment();
        return mfg;
    }

}

My PagerAdapter:

public class ViewPagerAdapter extends PagerAdapter 
{

    private static String[] titles = new String[]
    {
        "Page 1",
        "Page 2",
        "Page 3",
        "Page 4"
    };
    private final Context context;

    public ViewPagerAdapter( Context context )
    {
        this.context = context;
    }

    @Override
    public String getPageTitle( int position )
    {
        return titles[ position ];
    }

    @Override
    public int getCount()
    {
        return titles.length;
    }

    @Override
    public Object instantiateItem( View pager, int position )
    {
        TextView v = new TextView( context );
        v.setText( titles[ position ] );
        ((ViewPager)pager).addView( v, 0 );

        //This gives me nullPointerException///////////////////////////////////

        MyFragment mfg = new MyFragment();
        ((ViewPager)pager).addView(mfg.newInstance().getView());

        return v;
    }

    @Override
    public void destroyItem( View pager, int position, Object view )
    {
        ((ViewPager)pager).removeView( (TextView)view );
    }

    @Override
    public boolean isViewFromObject( View view, Object object )
    {
        return view.equals( object );
    }

    @Override
    public void finishUpdate( View view ) {}

    @Override
    public void restoreState( Parcelable p, ClassLoader c ) {}

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate( View view ) {}
}

What is the right way to add my MyFragment to the ViewPager? I have to add it as a view so I use the newInstance() method, but gives me a nullPointerException.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:52:46+00:00Added an answer on June 16, 2026 at 10:52 pm

    use FragmentPagerAdapter (not ViewPagerAdapter)and implement its

    Fragment getItem(int position){}
    

    with your Fragments

    return getNewInstance();
    

    and also dont forget to derive your activity from FragmentActivity class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using a Viewpager with 3 fragment. I would like to know if
I am using Jake Wharton's viewpager indicator and I would like to chage the
Hi I'm working with a view pager and fragments. I would like to override
I have a ViewPager with 3 Fragments and my FragmentPagerAdapter: private class test_pager extends
I'm using a pager adapter successfully but would like to know how to add
I am using the android ViewPager : http://developer.android.com/reference/android/support/v4/view/ViewPager.html This class allows you to basically
I have a FragmentActivity using a ViewPager to serve several fragments. Each is a
I am using Android's support.v4 package to develop a ViewPager containing multiple Fragment s.
In my activity, I'm using ViewPager to swipe left/right and show month view accordingly.
I am using a ViewPager with the FragmentStatePagerAdapter to allow navigation between some fragments.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.