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

Sexual Potatoes
Sexual Potatoes
12,051 Points

getChildFragmentManager() not being accepted

Whenever I try to use it, Android Studio says "FragmentPageAdapter in FragmentPageAdapter cannot be applied to android.app.FragmentManager.

I have followed the instructions of other questions that asked users to add a few things to their gradle file and none of them worked. The video seems to be outdated and with no notes. What should I do to get through this part?

6 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Without seeing all your code, it's very tough to help troubleshoot.

However, I just completed that section of the course this afternoon, and I had no issues. I am using the most current version of Android Studio, and all my files and dependencies are up-to-date, so I don't think the video is outdated. So, it sounds like you may have imported something wrong.

I would go over the last few videos, and make sure everything is imported correctly. (eg. you have android.support.v4.app.Fragment instead of just Fragment).

If you still can't find your error, you can download the project files for the video, which should bring you up to speed to where he is in the Video.

Hope you get it solved. :)

:dizzy:

Sexual Potatoes
Sexual Potatoes
12,051 Points

Hi, Jason, you hit the nail on the head, the problem was that my class extended Fragment and not android.support.v4.app.Fragment !

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points

Make sure your ViewPagerFragment class extends "android.support.v4.app.Fragmet", and not just "Fragment".

Boban Talevski
Boban Talevski
24,793 Points

@Rohan Tilekar. You should follow what Ben does in the video Managing the Back Stack to make all your Fragments extend from the v4 support library version of the Fragment class, along with FragmentManager and FragmentTransaction classes. There's also a change in the getFragmentManager method and you should use getSupportFragmentManager instead.

I got myself in the same confusing situation after leaving the Fragment classes as initially imported from the native Fragment class, which worked fine for the functionality of addToBackStack method as it didn't need the fix as in the video above. But at this point in the course, I'm setting them to be imported from the v4 support version because it seems the least confusing way to complete the course.

We need this because of the use of ViewPager and the FragmentPagerAdapter which are requiring the use of the v4 support version of the Fragment class as well. As someone mentioned above, you could extend the v13 support library version of the FragmentPagerAdapter class which allows the use of the native Fragment class, but then you would have to increase the minimum SDK of the project to 17 because it's required by the getChildFragmentManager method.

So all in all, it seems it's best to follow the video instructions even though the teacher notes at the above video say that the issue Ben presents in the video is already solved which suggests we don't need to make the change at that point in the course. But at this point, I think it's best to do so.

Stephen Wall
PLUS
Stephen Wall
Courses Plus Student 27,294 Points

Actually I was a bit hasty on my answer. I had to actually raise the min SDK version of my project to 17 in the build.gradle file (Module: app).

Then under dependencies, add the line: compile 'com.android.support:support-v13:+'

Then in your ViewPagerFragment.java file, when you are setting the adapter, make sure you are using the correct version of the FragmentPagerAdapter:

     ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager); 
     viewPager.setAdapter(new android.support.v13.app.FragmentPagerAdapter(getChildFragmentManager()) {
           @Override
           public Fragment getItem(int position) {
                   return position == 09 ? ingredientsFragment : directionsFragment; 
           }

Finally, within your IngredientsFragment.java file and your DirectionsFragment.java file, be sure to extend: android.app.Fragment

public class IngredientsFragment extends android.app.Fragment
Sam Kennedy
Sam Kennedy
2,449 Points

When I use just Fragment, I get an error on the line:

fragmentTransaction.replace(R.id.placeholder, viewPagerFragment, VIEWPAGER_FRAGMENT);

saying that viewPagerFragment should be of type android.app.Fragment instead. When importing just Fragment this error goes away but of course I then get the error Lucas has.

  1. My Ingredients and Directions fragments extend Fragment. 2.My MainActivity extends import android.app.FragmentManager; 3.The below code is ViewPagerFragment. this code // public Fragment getItem(int position) { // Fragment shows error.

getItem() , says is incompatible return type. What is the problem?

import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast;

/**

  • Created by rohan on 9/23/17. */

public class ViewPagerFragment extends Fragment { public static final String KEY_RECIPE_INDEX= "recipe_index";

@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
    int index = getArguments().getInt(KEY_RECIPE_INDEX);
    Toast.makeText(getActivity(),Recipe.names[index],Toast.LENGTH_SHORT).show();

    final IngrediantsFragment ingrediantsFragment = new IngrediantsFragment();
    final DirectionsFragment directionsFragment = new DirectionsFragment();

    View view = inflater.inflate(R.layout.fragment_viewpager,container,false);
    ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewpager);
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if(position == 0)
                {
                    return ingrediantsFragment;
                }
                else
                    return directionsFragment;
            }

            @Override
            public int getCount() {