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

Arnaud TOUTAIN
Arnaud TOUTAIN
957 Points

What are the modification needed to go through this step using Android Studio ?

I don't how to deal with the PlaceHolderFragment...

Seconding this post, looks like it may have changed again, I'm Googling but coming up empty at the moment

2 Answers

Mauricio Liu
Mauricio Liu
1,141 Points

Static classes have to be nested inside other classes. PlaceholderFragment is originally a nested class inside MainActivity class, when extracting it you can nest it inside the newly created SectionsPagerAdapter class for it to work.

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!