Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.+"