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

Fragments Ribbit Project: Eclipse auto created fragments and code differences

Working on the Self destructing message app part of the ribbit project and I'm having trouble working out what to do with the Fragments as my code seems quite different to the tutorial.

I've read through the suggested forum helper post, but it doesn't seem to have anything that can help me understand what I need to do.

I'm using the Eclipse bundle recommended by Google The main issues I have are:

  • Major code differences from what I have in my main_activity.java class file
  • Auto created fragment xml files in the res/layout directory

I have the following imports in my main_activity.java class file

+import java.util.Locale;
+import android.app.ActionBar;
+import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentManager;
+import android.app.FragmentTransaction;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.v13.app.FragmentPagerAdapter;
+import android.support.v4.view.ViewPager;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.widget.TextView;
+import com.parse.ParseUser;

Some of which are mentioned in the forum helper post, however, they appear to be updated versions such as android.support.v13.app.FragmentPagerAdapter, wherearse the helper posts mentions android.support.v4.app.FragmentPagerAdapter;.

Do I have all the relevant imports, if so which ones are they? If not, what imports do I need?

As for code differences, Ben's main_activity.java class is like this:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener{

}

Mine is like this and doesn't extend a FragmentActivity is this important to change and if so how do I do it?

public class MainActivity extends Activity implements ActionBar.TabListener {

}

Also I have several nested classes in my main_activity.java class that Ben doesn't seem to have, based on the videos, do I need to use these and if so how?

/**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            TextView textView = (TextView) rootView
                    .findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

The XML files I have in my res/layout directory include three fragment xml files, that Eclipse auto created, do I need to use them or create new xml files?

  • activity_login.xml
  • activity_main.xml
  • activity_signup.xml

Eclipse auto created files:

  • fragment_login.xml
  • fragment_main.xml
  • fragment_sign_up.xml

Any help on what I should do or how I need to adapt what's in the tutorial greatly appreciated.

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry for the confusion, Adam! The tools have been updated since I recorded this course a few months ago, and these differences can be a real pain. The crux of the issue is that the tools now create Fragments within Activities by default. We need to either adapt our code to run inside the Fragments instead, or delete the Fragments and use just Activities, like in the video. Either is fine but both require a few extra steps.

It sounds like you may already have the login and signup screens working, so let's focus on MainActivity. It should have Fragments, so have it extend FragmentActivity instead of Activity. The PlaceholderFragment that's in there is just the new version of DummySectionFragment in my code, which we see how to remove in Modifying Fragments from the Template. Work through that and then pop back in here with any follow-up questions.

I'll figure out how to add notes or update these videos for the future.

Thanks alot Ben, this helped.

For anyone else reading this I ran into a second problem, after this, with my SectionsPagerAdapter class where it was throwing an error on the return line of the getItem method.

@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(); //Error thrown here
    }

I found the solution here and here

I needed to make sure all my imports wre android.support.v4.app ones and not v13 and I needed to change the return type of the getItem method to a ListFragment instead of a fragment.

@Override

    public ListFragment 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(); //Error no longer thrown
    }

If you follow along with the videos this error stops you being able to refactor and rename the fragment_main.xml/dummy_fragment_main.xml file to fragment_inbox.xml and copy and create the friendsfragment class.