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) {
}

这就是我的错误所在。案例 1 getItem,类型不匹配:无法从 MainActivity.HistoryFragment 转换为 Fragment。它告诉我要么将方法返回类型更改为,要么将返回类型HistoryFragment更改newInstance()为片段。我也做不到的地方。我见过的其他示例看起来几乎与我的代码相同。我尝试过和不通过论点。

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

而我HistoryFragment的延伸ListFragment。最后,它不会从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));
    }
}
4

2 回答 2

26

睡了一觉后才明白。我正在导入 android.app.ListFragment 而不是 android.support.v4.app.ListFragment

于 2012-08-13T00:40:24.837 回答
0

我认为您必须在返回之前将类型转换为片段。所以试试这个

Fragment history = HistoryFragment.newInstance(i);
return  history;
于 2012-08-12T11:01:25.887 回答