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

Noam Elbaz
Noam Elbaz
5,672 Points

Android Studio and PlaceholderFragment

What do I do with Android Studio and the Placeholder Frag - which is not like the DummyFrag in the video. I have looked through the forum and did alot of what was mentioned. It got me further but not all the way.

Thanks.

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() {
    // Show 2 total pages.
    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;
}

1 Answer

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!