Android 无法应用 ViewPager FragmentManager 错误

Android ViewPager FragmentManager cannot be applied error

我以常规方式使用寻呼机视图。在下面的代码中,我得到了错误:android.support.v4.app.FragmentManager cannot be applied to android.app.FragmentManager。我为此检查了所有解决方案并实现了它们(导入似乎没问题),但我仍然有这个错误...

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class MyActivity extends FragmentActivity {

    private static int NUM_PAGES = 0;  //The number of pages to show.
    private static ViewPager mPager;  //The pager widget
    private static PagerAdapter mPagerAdapter;  //The pager adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mPager = (ViewPager) findViewById(R.id.pager);
        //Next line is error
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    /**
     * A simple pager adapter
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return MyFragment.create(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
    ...
}

使用 getSupportFragmentManager() 而不是 getFragmentManager()。根据您的 class 的导入,您正在使用来自支持库

Fragment