Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

why is eclipse showing a type mismatch

/**

  • A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  • one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter {

    protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) { super(fm); mContext = context; } @Override public Fragment getItem(int position){ //getItem is called to instantiate the fragment for the given page. //Return a DummySectionFragment (defined as a static inner class //below)with the page number and its lone argument.

    switch(position) {
    case 0:
        return new ChatFragment();
    case 1: 
        return new InboxFragment();
    case 2:
        return new FriendsFragment();
    case 3:
        return new DailyFragment();
    
    }
    
    return null;
    

    }

    @Override public int getCount() { // Show 4 total pages. return 4; }

    @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return mContext.getString(R.string.title_section1).toUpperCase(l); case 1: return mContext.getString(R.string.title_section2).toUpperCase(l); case 2: return mContext.getString(R.string.title_section3).toUpperCase(l); case 3: return mContext.getString(R.string.title_section4).toUpperCase(l); } return null; } }

This is what the error is showing: Type mismatch: cannot convert from ChatFragment to a Fragment Type mismatch: cannot convert from FriendsFragment to a Fragment Type mismatch: cannot convert from DailyFragment to a Fragment Type mismatch: cannot convert from InboxFragment to a Fragment

2 Answers

what do you mean by that a did SectionsPagerAdapter extends Fragment

Stone Preston
Stone Preston
42,016 Points

your method here is returning an object of type Fragment

public Fragment getItem(int position){
    //getItem is called to instantiate the fragment for the given page.
    //Return a DummySectionFragment (defined as a static inner class
    //below)with the page number and its lone argument.

    switch(position) {
    case 0:
        return new ChatFragment();
    case 1: 
        return new InboxFragment();
    case 2:
        return new FriendsFragment();
    case 3:
        return new DailyFragment();

    }

    return null;

}

your switch statement returns instances of ChatFragment, InboxFragment, FriendsFragment, DailyFragment. In order for the return type of the method to match the type of the object it returns, your custom classes that you return need to inherit from Fragment (so they need to have extends Fragment in their class headings)

the errors you are getting such as cannot convert from DailyFragment to a Fragment suggest you may not have subclassed Fragment in those classes.

Stone Preston
Stone Preston
42,016 Points

do ChatFragment and FriendFragment extend Fragment?