2

I've tried to use this answer to use a ViewPager with indicator inside a fragment, like so. (Just a mock up).
There's navigation spinner that changes the fragment. Each "page" would contain a ListView.

So basically I'm looking for:
SherlockFragmentActivity -> SherlockFragment -> ViewPager -> ListView

However, when I try and implement what I saw in the answer I get an error with findViewById();
I assume it's because I'm not getting the correct "context".

Here's my Fragment:

public class HomeViewFragment extends SherlockFragment {

    private ViewPager mPager;
    private HomePagerAdapter mAdapter;
    private TitlePageIndicator mIndicator;
    private Context context;


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

        context = getSherlockActivity().getApplicationContext();            

        mAdapter = new HomePagerAdapter(context);

        mPager = (ViewPager)context.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (TitlePageIndicator)context.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
    } 

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
Community
  • 1
  • 1
osxoep
  • 23
  • 4

1 Answers1

1

Step #1: Delete context = getSherlockActivity().getApplicationContext();

Step #2: Replace context.findViewById with getActivity().findViewById

Step #3: Replace all remaining occurrences of context with getActivity()

Step #4: Delete your onActivityCreated() implementation, as it is not doing anything

Step #5: Resolve that you will never again use getApplicationContext() unless you know specifically why you need it in a given circumstance

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • findViewById isn't a method in a fragment though, what should I do there? And my adapter needs a context passed to it, not a fragment. – osxoep Apr 15 '12 at 21:15
  • @osxoep: Sorry, I wasn't paying close enough attention. I have edited my answer. – CommonsWare Apr 15 '12 at 21:17
  • Thanks, that builds my code but now I've got another error. I know what I'm doing tonight now :) – osxoep Apr 15 '12 at 22:20