Problems with FragmentPagerAdapter

孤者浪人 提交于 2019-12-04 10:50:59

问题


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;
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/15414535/problems-with-fragmentpageradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!