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 9140929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:34:50+00:00 2026-06-17T09:34:50+00:00

This is my first time trying to implement a ViewPager . My app basically

  • 0

This is my first time trying to implement a ViewPager. My app basically has a listing and details page and I’d like the user to be able to swipe between the details. The details are based on an id that I pass in from the listing page. The problem is that I’m calling getActivity() in the details fragment, which is coming back null. Like I said, I’m new to implementing a ViewPager so this might be something obvious:

ListingFragment.class:

public class ListingFragment extends SherlockFragmentActivity
{
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;
    public int Id = 0;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        DetailsFragment df1 = new DetailsFragment();
        df1.Id = 1;
        df1.DisplayItems();
        fragments.add(lf);

        DetailsFragment df2 = new DetailsFragment();
        df2.Id = 2;
        df2.DisplayItems();
        fragments.add(lf);
    }

    private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {  
            return fragments.get(index);
        }  

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

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    private void DisplayItems()
    {
        PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
        ...
    }
}

UPDATE

I’ve updated my code to use static newInstance method and a Bundle to pass the id to the details fragment.

ListingFragment.class:

public class ListingFragment extends SherlockFragmentActivity
{
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;
    public int Id = 0;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        DetailsFragment df1 =  DetailsFragment.newInstance(1);
        fragments.add(df1);

        DetailsFragment df2 =  DetailsFragment.newInstance(2);
        fragments.add(df2);
    }

    private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

        public MyFragmentPagerAdapter(FragmentManager fm) {  
             super(fm);  
        }  

        @Override  
        public Fragment getItem(int index) {  

            return fragments.get(index);
        }  

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

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    private int detailId;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);         
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    public static DetailsFragment newInstance(int id) {

        DetailsFragment lf = new DetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        lf.setArguments(bundle);
        return lf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.listing, container, false);
        detailId = getArguments().getInt("id");
        DisplayItems();

        return view;
    }

    private void DisplayItems()
    {
        PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("value", false);
        ...
    }
}
  • 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-17T09:34:51+00:00Added an answer on June 17, 2026 at 9:34 am

    You shouldn’t be setting variables and calling methods that have to do with initialization from the activity like that.Id should be set using setArguments(...) and using a static newInstance(...) method to instantiate your fragment. You should call displayItems() in onCreateView(...) inside your Fragment, not from the attached Activity.

    See the official Android guide on Fragments for more information. You definitely should give the whole thing a read. http://developer.android.com/guide/components/fragments.html

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

Sidebar

Related Questions

This is my first time trying to implement an Ajax call in rails 3,
This is my first time trying to set up a database. My intent is
this is my first time trying to use document classes in AS3 and im
This is the first time I'm trying random numbers with C (I miss C#).
This is my first time using XML documents. What I'm trying to do is
This is my first time making a hash table. I'm trying to associate strings
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
I am trying to implement Enum into my code for the first time. I
I am trying to implement the MVP method for the first time, using WinForms.
I'm trying to implement a solution with c3p0 for the first time. I understand

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.