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 Build a Self-Destructing Message Android App Using Fragments for Tabs Modifying Tabs from the Template

Issues with PlaceholderFragments

This code gives me an error. I don't know how to fix it.

package com.cgscomputing.ribbit;
import android.os.Bundle;
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 PlaceholderFragment.newInstance(position + 1);
    }

    @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);
            case 2:
                return mContext.getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

3 Answers

Tyrel Hiebert
Tyrel Hiebert
14,739 Points

I struggled with this for a few hours. Finally, Ben's answer here really helped me.

In SectionsPagerAdapter.java, change PlaceholderFragment to MainActivity.PlaceholderFragment so that it looks like this

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

This allows it to reference the MainActivity for the PlaceholderFragment class.

Then in your MainActivity.java, make sure that your imports for Fragment, FragmentManager and FragmentPagerAdapter are referencing the v4 support library, like this

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

Instead of any of them being

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentPagerAdapter;

In the notes for this video, it tells you to update the FragmentActivity import, but not the others.

You may not need FragmentManager as it is grey (unused) in my project at this stage, but I left it there anyway as I am just glad that my app will run now.

I hope this helps. I was stuck with this same problem and this seemed to do the trick!

You've created code for 3 fragments when you have getCount() returning 2. You should change getCount() to return 3 to return all 3 fragments. You were getting the error because fragment no. 2 is out of bounds of the fragment count.

Jaroslav Vankat
Jaroslav Vankat
12,054 Points

You have got 2 fragments (getCount() returns 2) and in the switch case part there are definied three different conditions. I think this is the problem.