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 trial

Android Build a Self-Destructing Message Android App Using Fragments for Tabs Modifying Tabs from the Template

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

Error: Incompatible types in getItem() method in SectionsPagerActivity class

Hi,

just having a trouble with incompatible types in getItem() (return value) method in SectionsPagerAdapter class.

The error is following:

Incompatible types.
Required: android.support.v4.app.Fragment
Found: cz.jvankat.ribbit.MainActivity.PlaceholderFragment

SectionsPagerActivity.java:

package cz.jvankat.ribbit;

/**
 * Created by Jarisak on 9. 10. 2014.
 */

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.content.Context;
import java.util.Locale;


/**
 * A {@link android.support.v13.app.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 PlaceholderFragment (defined as a static inner class below).
        return MainActivity.PlaceholderFragment.newInstance(position+1);
    }

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

    @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);
        }
        return null;
    }
}

If needed I can past the PlaceholderFragment from the MainActivity but it's just what was automatically generated.

Thanks for your help.

1 Answer

Try changing return MainActivity.PlaceholderFragment.newInstance(position+1); to
return PlaceholderFragment.newInstance(position+1); If that doesn't help, let us know.

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

This was actually the line before I've edited it (it couldn't resolve the symbol), so I added the MainActivity in front of this.

All right. You can try checking out this thread and see if it helps.

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

It works, you've saved my day, thanks :)

Awesome! I'm glad. Keep it up.