Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Managing Fragments Adding the ViewPager

the constructor expects android.Support.v4.App.FragmentManager

Im having Android.App.Fragment with ViewPager but the FragmentPagerAdapter constructor expecting Android.Support.V4.App.FragmentManager. i tried compile 'com.android.support:support-v13:23.2.1' but still didnt work for me. maybe do i need to replace import statement in code? but which nd where?

You need to make sure your import statement at the top does not say

        import android.app.Fragment;

but instead says

        import android.support.v4.app.Fragment;

then you need to use the getChildFragmentManager(); method as the argument for FragmentPagerAdapter

For example:

        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
                    @Override
                    public Fragment getItem(int position) {
                        return (position == 0) ? ingredientsFragment : directionsFragment;
                    }

                    @Override
                    public CharSequence getPageTitle(int position) {
                        return (position == 0) ? "Ingredients" : "Directions";
                    }

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

The reason for this is because you are using the support library for your viewpager so you need to use the support library for the fragment as well

1 Answer

You need to include the v4 support library in your app's build.gradle file. For eg : compile "com.android.support:support-v4:18.0.+"