FragmentPagerAdapter使用ListFragment时getItem出现错误

7

我看了很多代码,但无法弄清楚这个问题。 http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

肯定是什么简单的问题。 我展示了大部分代码。错误在下一节中。

    public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

public static final int TAB_COUNT = 3;
public static InputMethodManager inputManager;

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

    // To control keyboard pop-up
    inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

}
    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

这就是我的错误所在。 在getItem的第1种情况下,类型不匹配:无法将MainActivity.HistoryFragment转换为Fragment。它告诉我要么将方法返回类型更改为HistoryFragment,要么将newInstance()的返回类型更改为Fragment。但我都做不到。我看到其他示例看起来几乎与我的代码相同。我已经尝试过传递和不传递参数。
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            Fragment tipCalc = new TipCalcFragment();
            return tipCalc;
        case 1:
            // Fragment history = new HistoryFragment();
            return HistoryFragment.newInstance(i); //ERROR HERE
        case 2:
            Fragment stats = new StatsFragment();
            return stats;
        }
        return null;
    }

我有一个扩展了ListFragmentHistoryFragment。最终,它将不会从String[]值中获取数据,而是从数据库资源中提取。我只是想先设置好结构并看看/玩一下布局。

    public static class HistoryFragment extends ListFragment {
    // public HistoryFragment() {
    // }

    String[] values = new String[] { ... };

    static HistoryFragment newInstance(int num) {
        HistoryFragment history = new HistoryFragment();

        Bundle args = new Bundle();
        args.putInt("num", num);

        history.setArguments(args);

        return history;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.history, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1,
                values));
    }
}
2个回答

26

睡醒后我理解了问题所在。我是在导入android.app.ListFragment而不是android.support.v4.app.ListFragment。


1
将项目框架设置为2.3.3(如果您不需要3+的条件内容),这样您就可以避免此类问题。 - sherpya
我也遇到了完全相同的问题!我正要提问,结果看到了这个问题!!非常感谢!! - BlackHatSamurai
我有一个愚蠢的问题:为什么你一定要这样做?为什么你必须使用v4支持包而不是普通的标准包? - Eugene van der Merwe
@EugenevanderMerwe 我同意你的问题,为什么要这样做!我陷入了困境,而且非常恼火,为什么我需要使用支持库,实际库不能完成其预期工作吗? - AutoM8R
@EugenevanderMerwe,我刚刚发现原因所在。FragmentPagerAdapter只是支持库的一个类。因此,您需要确保您也使用了“support”片段,因此无论您在哪里构建新片段,都必须确保该片段正在使用支持库的导入。 - AutoM8R
显示剩余2条评论

0

我认为你需要在返回之前进行片段类型转换。所以尝试这个

Fragment history = HistoryFragment.newInstance(i);
return  history;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接