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

2 serious errors. please help!!

hi everybody

i am having to challenges, errors one is in the MainActivity.class and the other is in the SectionsPagerAdapter.class

the first problem goes like this:

my "final ActionBar actionBar = getSupportActionBar();" statement is giving me an error saying "cannot resolve method getSupportActionBar();"

here is my MainActivity code:

package com.muzinda.kgwaindepi.ribbit;

import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; 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.Menu; import android.view.MenuItem;

import com.parse.Parse; import com.parse.ParseAnalytics; import com.parse.ParseObject; import com.parse.ParseUser;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

/**
 * 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;
private ActionBar supportActionBar;


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

@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    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));
    }





    setContentView(R.layout.activity_main);
    ParseAnalytics.trackAppOpened(getIntent());

    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser == null) {


        navigateToLogin();

    } else {
        Log.i(TAG, currentUser.getUsername());
    }
    ParseObject object;
    // Enable Local Datastore.
    Parse.enableLocalDatastore(this);

    Parse.initialize(this, "jTxlNT63Iov1xTzImCOQiuAKfxDsVcjvbkPHwWvZ", "v7kKmfk2z23HWnVN2MGWsLovokYzLeHOK9vx09bD");


}

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.menu_main, 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();

    //noinspection SimplifiableIfStatement
    if (itemId == R.id.action_logout) {
        ParseUser.logOut();
        navigateToLogin();

    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

}

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

}

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

}

}

here is problem number 2 : the "return new InboxFragment();" its saying Incompatible Types required :android support v4 fragement found : com.appname.inboxFragement i have made the import you instructed me to add in the forum thus the "import android.support.v4.app.ListFragment;" but its graying out immediately and the error remains here is my code for InboxFragement.java

package com.muzinda.kgwaindepi.ribbit;

/**

  • Created by kgwaindepi on 6/11/2015. */

import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.ListFragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter;

import java.util.Locale;

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

3 Answers

my question is, how do i get rid of my two errors: 1) my "final ActionBar actionBar = getSupportActionBar();" statement is giving me an error saying "cannot resolve method getSupportActionBar();"

2) the "return new InboxFragment();" its saying Incompatible Types required :android support v4 fragement found : com.appname.inboxFragement i have made the import you instructed me to add in the forum thus the "import android.support.v4.app.ListFragment;" but its graying out immediately and the error remains here is my code for InboxFragement.java

thank you in advance

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Hi, can you please add the question? Thanks!

For question (1), try these imports in my MainActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.ParseUser;

I tried the imports suggested in the Teacher's Notes and also by other students in the forum but they didn't work, probably because there have been changes in the API since then. So, I created a project choosing Tabbed Activity (instead of a Blank Activity) with Action Bar Tabs (with ViewPager) as Navigation Style. This new project runs fine without any problems and shows the dummy tabs. I copied the imports from MainActivity in this new project to my MainActivity.

For question (2), I have these imports in InboxFragment.java:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.ListFragment;