9

I'm trying to make a slidescreen with viewpager and fragments so that I can load different layouts per fragment and give each page different functionality.

I followed a tutorial to accomplish this.

The error I'm getting when hovering over public Fragment getItem(int arg0): The return type is incompatible with FragmentPagerAdapter.getItem(int)

and error #2: The constructor FragmentPagerAdapter(FragmentManager) is undefined --> getting this one when hovering super(fm);

package com.example.spui;

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

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 5;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0+1);
        myFragment.setArguments(data);
        return myFragment;
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }
}
madlymad
  • 6,367
  • 6
  • 37
  • 68
mXX
  • 3,595
  • 12
  • 44
  • 61

1 Answers1

30

You are using the wrong FragmentManager import. Use android.support.v4.app.FragmentManager instead.

Same problem with Fragment - use android.support.v4.app.Fragment

Note: if you are building an API11+ only application and want to use the native Fragments, then you should instead change your FragmentPagerAdapter import to android.support.v13.app.FragmentPagerAdapter.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Awesome!! Thanks for the very fast response! That fixed out my errors :D. can you point me in the right direction to use different layouts for every page? Or should I make a new question for this, Because still trying to figure that one out Btw. "You can accept an answer in 3 minutes" – mXX Mar 14 '13 at 16:23
  • getItem can return any Fragment you want - if you want totally different functionality on each page, just return a different Fragment based on the page number. – ianhanniballake Mar 14 '13 at 16:33
  • Do I do this in MyFragment or in MyFragmentPagerAdapter? I update the question with a new class so you can see 'MyFragment' What is difference between support v13 and v4? – mXX Mar 14 '13 at 21:22
  • 1
    Support v13 uses the android.app versions of Fragment, FragmentManager, etc. that were introduced in API v11, rather than the android.support.v4.app versions which are available for all API levels. – ianhanniballake Mar 14 '13 at 21:39
  • And additional questions/issues should be asked as separate StackOverflow questions. – ianhanniballake Mar 14 '13 at 21:39
  • Thanks for the information about the versions. Ow okay, I'll ask a new question then and I'll delete the added question. Thanks for your help @ianhanniballake – mXX Mar 14 '13 at 21:41