android.support.v4.app.FragmentPagerAdapter cannot be applied to android.app.FragmentManager

Nikhil picture Nikhil · Mar 7, 2015 · Viewed 14k times · Source

I want to implement just 2 fixed tabs in my application. I followed this tutorial.

In my TabPagerAdapter class I'm getting this error :

FragmentPageAdapter in android.support.v4.app.FragmentPagerAdapter cannot be applied to android.app.FragmentManager

TabPageAdapter class:

package com.nikhil.tabs;


import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;


public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm); //getting error here
    }

    @Override
    public Fragment getItem(int index) { //getting error here

        switch (index) {
            case 0:
                // Top Rated fragment activity
                return new TopRatedFragment();
            case 1:
                // Games fragment activity
                return new GamesFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 2;
    }

}

Why is that error caused and how to solve it?

Answer

Apurva picture Apurva · Mar 7, 2015

Error occurs because your FragmentPagerAdapter uses support libraries and Fragment and FragmentManager don't!

Change

import android.app.Fragment;
import android.app.FragmentManager;

To

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;