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 trialClement Besson
83 PointsProblem 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!
8 Answers
Tom Schinler
21,052 PointsGREAT 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!!!!!!
Ashley Martis
1,525 PointsEurica, this is awesome thanks.
Clement Besson
83 PointsThat worked thanks a lot for your help!
Aloke Desai
199 PointsLooks 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?
Aloke Desai
199 PointsBump, getting this error as well
Clement Besson
83 PointsAre you still getting the error Aloke?
Aloke Desai
199 PointsNot 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.
himanshu saini
567 PointsUse this statement - final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
Tom Schinler
21,052 PointsSo 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
21,052 PointsOk 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.
Andy Hammond
5,415 PointsAparently we need it to be "extends FragmentActivity"
what happens if we change it back to ActionBarActivity?
Ratul Sarna
Courses Plus Student 11,618 PointsRatul Sarna
Courses Plus Student 11,618 PointsCan you please provide the details of the error you're encountering?