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

Problem setting up the action bar in Android Studio

Hey everyone,

I am coding the app in Android Studio and at this point I am having an error showing up in the MainActivity.java.

Here are my imports:

import java.util.Locale; import android.app.Activity; import android.content.Intent; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

and here is where I have an error showing up:

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

it cannot resolve the getSupportActionBar()

I must have done something wrong but I did not find what... Could someone help me?

Thanks!

Ratul Sarna
Ratul Sarna
Courses Plus Student 11,618 Points

Can you please provide the details of the error you're encountering?

8 Answers

Tom Schinler
Tom Schinler
21,052 Points

GREAT SCOTT!!!!!!!!!!!!

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

needs to be changed to:

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener

because ActionBarActivity provides the getSupportActionBar()

Thank you Stack Overflow!!!!!!

Eurica, this is awesome thanks.

That worked thanks a lot for your help!

Looks like that's fine since ActionBarActivity is a subclass of FragmentActivity. This should maybe be updated in the notes in the teacher guide on how to fix this issue?

Bump, getting this error as well

Are you still getting the error Aloke?

Not sure if Clement is having the exact same problems, but my problem seems to be that MainActivity currently extends ActionBarActivity. My current class header is:

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener

when I try to change MainActivity to extend FragmentActivity instead, I get the error that Clement is talking about.

Use this statement - final android.support.v7.app.ActionBar actionBar = getSupportActionBar();

Tom Schinler
Tom Schinler
21,052 Points

So here is the code I have in MainActivity.java:

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;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {


    public static final String TAG = MainActivity.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_main);

        ParseAnalytics.trackAppOpened(getIntent());
        ParseUser currentUser = ParseUser.getCurrentUser();
        if(currentUser == null) {
            navigateToLogin();
        }
        else {
            Log.i(TAG, currentUser.getUsername());

        }
        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();  // Error-Cannot resolve method
        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()); // Error- SectionsPageAdapter() in  SectionsPageAdapter cannot be applied to:

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

//Rest of code omitted

these are the only errors I am getting. It seems as if MainActivity.java cannot see the getSupportActions() nor read the SectionsPageAdapter class.

Tom Schinler
Tom Schinler
21,052 Points

Ok so... in SectionsPagerAdapter.java changing :

public SectionsPagerAdapter(Context context, MainActivity mainActivity, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

to:

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

(getting rid of the "MainActivity mainActivity")resolves the SectionsPagerAdapter() error.

Still working on the getSupportActionBar() error though.

Aparently we need it to be "extends FragmentActivity"

what happens if we change it back to ActionBarActivity?