I have the following code for my fragment. I pretty much took this from a basic view pager example and slapped it into a fragment to test for a project I’m working on.
I’m using ACtionBarSherlock, and viewPagerIndicator libraries. The Fragment below contains a viewPager that in this example is supposed to swipe between simple title strings.
I tried the example with the ViewPager and ViewPagerIndicator in an activity, and no problem. But the second I put it into a fragment. BOOM, nothing but a black screen… NO errors, just a black screen…I feel like I must be inflating, or accessing the layout incorrectly or something…
Any ideas?
Here’s the code:
public class TestFragment extends SherlockFragment {
//~Constants----------------------------------------------
//~Data Fields--------------------------------------------
//~Constructors--------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
//Setup view
super.onCreate(savedInstanceState);
View view = onCreateView(getLayoutInflater(savedInstanceState), null, savedInstanceState);
ViewPagerAdapter adapter = new ViewPagerAdapter( this.getSherlockActivity() );
ViewPager pager =
(ViewPager)view.findViewById(R.id.free_time_day_pager);
TitlePageIndicator indicator =
(TitlePageIndicator)view.findViewById(R.id.titles);
pager.setAdapter( adapter );
indicator.setViewPager( pager );
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_time_fragment_layout, null, false);
return view;
}
//~Methods-------------------------------------------------
}
Dont call
onCreateViewthat is wrong.onCreateViewis an overridden method which is called by theFragmentMangerto handle theFragmentlifecycle.Also be aware if you are using a FragmentPagerAdapter you require a special method to create the ViewPagerFragment, its a bit tricky but refer to this gist.
Read about
FragmentLifecycles here too.Cheers