FragmentPagerAdapter getItem error with ListFragment

user1235035 picture user1235035 · Aug 12, 2012 · Viewed 8.1k times · Source

I've looked at quite a lot of code and can't figure this out. http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

It has to be something simple. I'm showing most of the code. The error is in the next section.

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

This is where my error is. case 1 of getItem, Type mismatch: Cannot convert from MainActivity.HistoryFragment to Fragment. it is telling me to either change method return type to HistoryFragment or change return type of newInstance() to Fragment. Where I can't do either. Other examples I've seen look almost identical to my code. I have tried with and without passing an argument.

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

And my HistoryFragment that extends ListFragment. In the end it won't be pulling from the String[] values but from database resources. I just wanted to setup a structure first and see it/play with the layout.

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

Answer

user1235035 picture user1235035 · Aug 13, 2012

I figured it out after getting some sleep. I was importing android.app.ListFragment instead of android.support.v4.app.ListFragment