Al Lelopath
Al Lelopath

Reputation: 6778

Type mismatch: cannot convert from ListFragment to Fragment

I have a TabsPagerAdapter class as below. The classes FirstFragment, SecondFragment and ThirdFragment each extended Fragment and all was well. Then I changed SecondFragment to extend ListFragment and now I get compile error:
Type mismatch: cannot convert from SecondFragment to Fragment

ListFragment extends Fragment, so in my mind. this shouldn't be a problem.
What do I do?

Import in SecondFragment is:

import android.app.ListFragment;

Class:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;

    public TabsPagerAdapter(FragmentManager fragmentManager, Context context) {
        super(fragmentManager);
        mContext = context;
    }

    @Override
    public Fragment getItem(int index) {

        Fragment returnFragment = null;

        switch (index) {
        case 0:
            returnFragment = new FirstFragment(mContext);
            break;
        case 1:
            returnFragment=  new SecondFragment(mContext);
            break;
        case 2:
            returnFragment = new ThirdFragment(mContext);
            break;
        }

        return returnFragment;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 3;
    }
}

Upvotes: 2

Views: 1033

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

The issue is usually due of a mismatch between the import in the different classes. You probably have the native Fragment in one class and the one from the support library in the other. Fix the imports and it will work. check the imports. Also remove the constructor that takes a parameter in your Fragment subclasses. The system wants a public empty constructor (aka default constructor). If you need a context, you can use getActivity()

Upvotes: 3

Related Questions