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

Erin Kabbash
Erin Kabbash
5,210 Points

SectionsPagerAdapter Error

Hello,

I am following along in the Ribbit app and there are a bunch of differences in the Eclipse generated code and the video. I believe I cleaned most of them up and followed solutions on the forum but I am still recieving one final error in the SectionsPagerAdapter.java file. The error I am getting when I hover over is: The return type is incompatible with FragmentPagerAdapter.getItem(int). On the line: public ListFragment getItem(int position) {

Here is my full code for the SectionsPagerAdapter.java file:

package com.erinkabbash.ribbit;

import java.util.Locale;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;

/**
 * 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 ListFragment 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 new InboxFragment(); 
    }

    @Override
    public int getCount() {
        return 2;
    }

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

        }
        return null;
    }
}

Any help would be appreciated because I am lost.

Thanks!

2 Answers

Erin Kabbash
Erin Kabbash
5,210 Points

Ok.. I finally figured out my issue. It was a few things so I will explain in case someone else runs into this issue. In my imports I was importing some v4 and some v13, they all need to match in every file in the project as they do not play nice together. Once I had that solved it fixed my issue but created another.. which was the error "The constructor FragmentPagerAdapter(FragmentManager) is undefined" which can be fixed by going back to MainActivity and changing the "mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager());" line to: " FragmentManager fragmentManager = getSupportFragmentManager(); mSectionsPagerAdapter = new SectionsPagerAdapter(this, fragmentManager); " and then organize your imports. Hopefully this helps someone because I was definitely stuck for a few days on this one!

Hello Erin Kabbash , Am stuck here, am using eclipse too..

Thanks

Chibueze Oti
Chibueze Oti
Courses Plus Student 9,108 Points

Yes this helped but also i had to change Activity to AppCompatActivity else it wont find getSupportFragmentManager()