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

This tutorial is really out of date, on android studio none of the fixes work, and the code is very different.

the fixes to allow the tutorial code to work in android studio do not fix anything, most of them are actually syntax errors. example

mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
``

cannot be replaced with 

```java
        mSectionsPagerAdapter = new SectionsPagerAdapter(getsuppoFragmentManager());

omg I thought this was a ticket submission, I really hope this isn't a new forum thread...

and it's a forum thread.

Harry James
Harry James
14,780 Points

Hehe! Yes this is a forum thread but your lucky it is because Patrick Corrigan was correct with his statement that you're missing an argument in the constructor. I've modified Patrick's code slightly to fix a typo that you put in getSupportFragmentManager() which would also give you an error :)

Harry James
Harry James
14,780 Points

Hey David!

I've gone ahead and tested the code and it seems to work for me.

What version of the Android SDK are you using to compile with?

Compile version is 20

What version should I be using for this?

Harry James so now, I switched to 19, which doesn't work either. It just feels weird using older api for a new project.

Harry James so now, I switched to 19, which doesn't work either. It just feels weird using older api for a new project.

3 Answers

You're missing an argument in the constructor. Try this:

mSectionsPagerAdapter = new SectionsPagerAdapter(this, getsupportFragmentManager());

unfortunately, that code still doesn't work in that context. Try downloading the source yourself, it's broken.

See my question and answer "This Is The Droid You Are Looking For" for complete updated code and explanation.

Harry James
Harry James
14,780 Points

It shouldn't make a difference what SDK you use but I wanted to test the SDK on my side to see if everything ran fine (Which it did)

Can you verify that you spelt SectionsPagerAdapter correctly where you are calling the method. Also, please check your SectionsPagerAdapter class's constructor, it should look like this:

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
    }

If you are still having problems, please let me know what the error message is where you call the method.

im still having this null pointer exception in this line mViewPager.setAdapter(mSectionsPagerAdapter);}

please help, im using Android Studio

Harry James
Harry James
14,780 Points

Hey Juan! Could you please show me your mViewPager and mSectionsPagerAdapter variable declarations?

Harry, I'm spending time before finishing this video trying to sort out the compat errors I'll run into for android studio, but the little snippet you've provided here has an mContext variable that I don't see declared in the boiler plate code. Is that where their null pointer exception is coming from, and if so, where in the scope are we declaring this context, where is it coming from?

Harry James
Harry James
14,780 Points

Hi there Nicolas Hampton!

Sorry about that, the mContext variable is not needed - it is only used for setting the tabs titles if you are getting Strings from the strings.xml file (These strings must be accessed from the context).

I've reflected my answer above to show this.


Here are what the classes should look like depending on whether you're using the context or not:

Using context

SectionsPagerAdapter.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {

protected Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }
// ... additional code

Class is then called like this:

mSectionsPagerAdapter = new SectionsPagerAdapter(this,
                getSupportFragmentManager());

Not using context

SectionsPagerAdapter.java
public class SectionsPagerAdapter extends FragmentPagerAdapter {

protected Context mContext;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
// ... additional code

Class is then called like this:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

Hopefully this should clarify some things up but, if you're still having issues with your SectionsPagerAdapter then please do post your code here so that we can take a look :)