Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLend Kazazi
223 Pointswhy 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
Lend Kazazi
223 Pointswhat do you mean by that a did SectionsPagerAdapter extends Fragment
Stone Preston
42,016 Pointsdo ChatFragment and FriendFragment extend Fragment?
Stone Preston
42,016 PointsStone Preston
42,016 Pointsyour method here is returning an object of type Fragment
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.