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

J Smillie
J Smillie
39,714 Points

Hi everyone I have been through the questions and answers regarding the difference between this and android studio...

... and I just can't seem to figure out what is going on. I would really appreciate it if someone could help me get back on track with my code in Android Studio.

Here is what I have for the MainActivity(MyActivity):

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
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 com.parse.ParseAnalytics;
import com.parse.ParseUser;

import java.util.HashMap;
import java.util.Map;


public class MyActivity extends FragmentActivity{


    public static final String TAG = MyActivity.class.getSimpleName();

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        Map<String, String> dimensions = new HashMap<String, String>();
        // What type of news is this?
        dimensions.put("category", "politics");
        // Is it a weekday or the weekend?
        dimensions.put("dayType", "weekday");
        // Send the dimensions to Parse along with the 'read' event

        ParseAnalytics.trackEvent("read", dimensions);

        ParseUser currentUser = ParseUser.getCurrentUser();

        if (currentUser == null) {

            navigateToLogin();
        }
        else {
            Log.i(TAG, currentUser.getUsername());
        }

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

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

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }
    }



    private void navigateToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int itemId = item.getItemId();
        if (itemId == R.id.action_logout) {
            ParseUser.logOut();
            navigateToLogin();

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }



    /**
     * 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_my, container, false);
            return rootView;
        }
    }

}

The SectionsPagerAdapter isn't recognised and the method getSupportActionBar is missing... I just really don't know how to approach debugging this now... maybe it is something to do with my imports but 3/5 errors I get are about a 'supertype'.

Can anyone help. Apologies if it seems like I'm dumping code. Spent a lot of time here and now the code seems to have gone wild in one swift move...

3 Answers

J Smillie
J Smillie
39,714 Points

Thanks Ben. Managed to get to the end of the course with a working app running alongside! I'm very happy, and I really appreciate your help. Had a little trouble with importing the correct files for InboxFragment and FriendsFragment but found other forum posts concerning this very informative and helpful.

Looking forward to getting on with the rest of the course now :)

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry for my late reply here. Can you tell if your project is successfully using the appcompat_v7 project? Do you get any errors with your imports? This is an annoying difference that came about after this project was recorded.

If you want, zip up your project and link to it here (via Dropbox or something) or send it to us at help@teamtreehouse.com and I can take a look.

J Smillie
J Smillie
39,714 Points

Hi Ben,

Thanks for getting back to me! I can't be entirely sure if there is an issue with my imports the main issue seems to be with the usage of SectionsPagerAdapter and with the action bar.

Can you tell me how to tell if I'm successfully using appcompat_v7 project?

Here is a link to a dropbox folder with my app in if you or anybody would like to take a look!

https://www.dropbox.com/sh/vsves5jgl9x0a6e/AABq0tg8EiynjAWq4Kof-Fu4a?dl=0

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Oh! You're missing the whole SectionsPagerAdapter class. You should have created it in this video: http://teamtreehouse.com/library/build-a-selfdestructing-message-android-app/using-fragments-for-tabs/modifying-tabs-from-the-template

Check that out, and you can download the project files from the "Downloads" tab there and grab the file. Copy it into your com.wolfonmoon.how package and you should be set.