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 Fragments from the Template

john appleby
john appleby
2,773 Points

Android studio (return new InboxFragment)

Hi I've been following along making good progress until now, when I try to add the "return" statement SectionsPagerAdapter.java I am getting an error and the app won't compile for testing. here is a snippet of my code thus far.

 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 new InboxFragment();     


        }

and these are my options Make "getItem" return "package name.InboxFragment"

Required: android.app.fragment

Please help

John

12 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

This is a frustrating thing about working with the ViewPager. :-/

First things first: there are two versions of all the Fragment classes: android.app fragments and android.support.v4.app. fragments. Regular fragments (android.app) can be used from API version 3.0 and beyond. The support versions (android.support.v4.app) are backwards compatible with older versions of Android (before version 3.0).

The Ribbt app is targeting 4.0 and beyond. However, the ViewPager comes from the support library and requires the older version of the Fragment classes.

So the solution is to make sure that InboxFragment and FragmentPagerAdapter both have import statement for the support versions, not the regular versions.

InboxFragment.java and FriendsFragment.java:

import android.support.v4.app.ListFragment;

FragmentPagerAdapter:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
john appleby
john appleby
2,773 Points

You ben are a genius in your own right ! Thank you so much... I can carry on with the learning now.

P.s your videos are very good, keep up the good work .

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Excellent, and thanks! Hope you enjoy the rest of the project. :)

Masum Bergmann
Masum Bergmann
Courses Plus Student 4,129 Points

Thanks for this Ben!

Just as a side note: I did not have a FragmentPagerAdapter class but rather a SectionsPagerAdapter class to which I added these imports.

My fragmentPagerAdapter is using app.android.Fragment and i cant edit that! it is read only fiile and i cant edit that so when i use super(fm) it gives me eror of wrong type.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Oz, can you start a new thread about this? Tell me which file is read only and perhaps paste your code in and more details about your errors.

Nahuel Miranda
Nahuel Miranda
2,048 Points

Hey Ben, I'm having a similar issue in my code but I'm afraid I don't quite understand the solution you're posting. Here's the contents of my SectionsPagerAdapter.java file (in "return new InboxFragment()" the error is: "cannot resolve symbol InboxFragment()")

/**

  • Created by nmiranda on 9/10/2014. */ 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 new InboxFragment(); // in the line above the error is: "cannot resolve symbol InboxFragment()" }

    @Override public int getCount() { // Show 3 total pages. return 3; }

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

Thanks for your input!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry for the confusion here! Have you tried all the steps listed in the Teacher's Notes on that video page? It's usually a problem with the import statements at the top of your classes.

Here's the other Forum post that has the solution used in the Teacher's Notes: https://teamtreehouse.com/forum/feeling-lost-regarding-default-fragments-following-the-adt-update

hey Ben, i just did that in my InboxFragment.java class, i added the "import android.support.v4.app.ListFragment;" but the import statement immediately grayed out leaving the error still there

john appleby
john appleby
2,773 Points

After trailing the internets dark corners and looking deep into other peoples codes I found a repetitive solution ... but I'm not sure if this will effect the flow of our project... is this an acceptable edit or will it mess things up ?

public class MainActivity extends FragmentActivity implements ActionBar.TabListener

I'm going to carry on with the lesson now but I'm hoping I haven't messed it up .

John

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It should be FragmentActivity...was it not? That would definitely cause a problem!

I was in the same situation, once I changed Activity to:

public class MainActivity extends FragmentActivity

I was able to set mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

Seye Kuyinu
Seye Kuyinu
2,581 Points

With the new version of ADT, it wasn't FragmentActivity. I had to change it myself from Activity to FragmentActivity

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Ha!

This code should have been generated for you, but it's easy to mix it up for one reason or another. Instead of getFragmentManager() it should be getSupportFragmentManager(), like this:

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

There is generally a 1 to 1 conversion between regular and support versions like this. :)

Harry James
Harry James
14,780 Points

Thanks again Ben, had this problem as well and this fixed worked too :D

Full of fixes here! However, just to say, I started getting errors on this video: http://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/using-fragments-for-tabs/modifying-fragments-from-the-template and you include this post in the video before that.

I assume that the different problems listed on this post occur at either the video the notes are on now or the video I mentioned so, to make it easier for people to find, putting a link in the notes on the video above would probably help too.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for the heads up! Adding the link now. :)

Kerry Levenberg
Kerry Levenberg
4,550 Points

Just a quick note, the latest version of Eclipse (w/ Android SDK 22.6.2) also automatically includes fragments, and the same adjustments needed for Android Studio also work in Eclipse.

john appleby
john appleby
2,773 Points

Oh the joys of coding.... what have I got myself into here, I wouldn't mind a 1 on 1 conversation with this Fragment Manager myself :0)

I knew that "android studio" would have some bugs, but not an ant farm worth of them ! Thanks for your time helping me with this issue "obi BEN kenobi" but that last fix didn't seem to do it. now I get an error :-

error: cannot find symbol method getSupportFragmentManager()

the quick-fix is asking me if I want to :- Create Getter. Create Property. Create Method "getSupportFragmentManager"

Ive tried shouting at the dog and that didn't seem to work, but I got some satisfaction :0)

Thanks again for your time Ben it is much appreciated .

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Poor John! It can be a real wormhole. :)

When stuck like this, try comparing with the project files from that video or stage. I think this line is what you need:

import android.support.v4.app.FragmentActivity;

john appleby
john appleby
2,773 Points

I did already have that line in my imports .... and yes I wormhole is a good description of the place where I end up :0)

Thanks again for your help and the little fix below seems to be working it compiled ok and runs...... fingers crossed

Nathaniel Janick
Nathaniel Janick
11,671 Points

I ran into similar issues with the code at this point, using Android Studio. My java jargon isn't up to snuff, but I think the issue has to do with the difference between how Eclipse builds the initial project versus the gradle build.

The Android Studio version of this project, following all of the same steps, uses the term PlaceholderFragments instead of Dummy Fragments, and some of the code that needs to be moved from the MainActivity class to SectionsPagerAdapter is not in quite the same order as the Eclipse version which makes the copy+paste portion of this project moderately complicated. I wish I could post the code for comparison, but I was so flustered trying to fix the code that by the time I had cleaned up all the errors Control+Z was out of the question. I can concur with John though that the original MainActivity class extends Activity rather than FragmentActivity. The errors were much more manageable after catching that snafu.

It wasn't too painful to fix up the errors and having watched subsequent videos in the project, the directions seem to work smoothly other than this issue.

Ben: It may already exist and I just can't find it, but you might consider adding alternate instructions for those of us making the leap to Android Studio. Not necessarily a video, but maybe a warning in the notes section. The step where everything gets nasty in Android Studio is "Modifying Tabs from the Template."

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for the feedback - I will definitely do that! I'll write something up and link to it in the Teacher Notes section.

Seye Kuyinu
Seye Kuyinu
2,581 Points

I don't know but I still get thrown the same error at new InboxFragment(). Tried everything. Here's my code:

package com.seyekuyinu.ribbit;

import java.util.Locale;

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

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

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

}

john appleby
john appleby
2,773 Points

Well I thought I was up and running but after inserting those imports into the relevant classes I now have a problem in the MainActivity ..... mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

where the (getFragmentManager()) seems to be the problem. what should I cast here ?

please help me Ben your my only hope :0)

john appleby
john appleby
2,773 Points

all is well thanks for the guidance .

John

Timothy Boland
Timothy Boland
18,237 Points

Phew, with Android Studio that was a tough one to workaround....Persistence Pays :-)

Tom Wierman
Tom Wierman
3,002 Points

I picked and chose several of the ideas. The changing Activity to FragmentActivity seems to have done the job. However, I cannot say all of this is registering.

Make sure the class is under the right package! it fixed my problems