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 Sending Messages Selecting Recipients

Wei-Ting Chen
Wei-Ting Chen
7,632 Points

Action Bar not show up in RecipientsActivity and EditFriendsActivity

Hi everyone, I am using Android Studio 1.0, my action bar doesn't show up in the RecipientsActivity and EditFriendsActivity. The code from menu_recipient.xml is down below:

``Android <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="co.sensenow.ribbit.RecipientsActivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" /> </menu>

The code from RecipientsActivity.java is down below:

``public class RecipientsActivity extends ListActivity {

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

protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendRelation;
protected ParseUser mCurrentUser;


@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipients);

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

@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_recipients, menu);
    return true;
}

Any help will be appreciated!

6 Answers

For both activities, in your onCreate the next line after setContentView( ...
add: getActionBar().setDisplayHomeAsUpEnabled(true); in the manifest for both activities add android:theme="@android:style/Theme.Holo.Light" in the tag directly after android:screenOrientation="portrait"

save and run

Charles Li
Charles Li
15,557 Points

Thanks a lot Billy!!! I have been solving my NullPointerException and this finally worked using your solution! My Action Bar is there now but I actually run into a different problem. My Action Bar has a three dots icon on the very right side of it and my "Send" option only appears after I press on that. And on top of it, it only shows as text not an icon (maybe because it's not on the Action Bar itself). If you have any insights on this please let me know. Thanks so much already for letting me advance through the courses!

By the way, the three dots icon only shows up after I choose a recipient.

babono nurul akbar
babono nurul akbar
5,731 Points

i also have the same problem.. have you figured it out the solution?

Charles Li
Charles Li
15,557 Points

I been searching all over the web but I couldn't find any solutions... One person said that showAsAction doesn't work when ActionBarActivity isn't extended. Maybe that's why... but I didn't try it because I still want to extend ListActivity like what the Instructor did. Hopefully someone else can help us out soon. For now, I'm just gonna work on the app even with this problem.

You should try using the Android Studio version to create an Activity with a Fragment, then extend the fragment for a ListFragment and do all the coding in there. It should naturally keep the ActionBar and allow you to use the automated list funcionality android provides.

It appears that you are listing the the showAsAction, as "never" instead of "always"

android:showAsAction="always"

Wei-Ting Chen
Wei-Ting Chen
7,632 Points

well even I used "always",the action bar still not show up :(

Ratul Sarna
PLUS
Ratul Sarna
Courses Plus Student 11,618 Points

The issue might be the theme your app is using. Check if in values/styles.xml, the app theme is AppCompat. If it is so then your Activity needs to extend ActionBarActivity instead of ListActivity. But then you would not get the free ListView in the Activity. I would first suggest to check your app's theme as that might be the source of the problem. If it is so then I can reply to suggest a fix.

Ariel Borochov
Ariel Borochov
Courses Plus Student 7,436 Points

How to change the theme? I have this issue. I can either inherit list activity or actionbar activty

Ratul Sarna
Ratul Sarna
Courses Plus Student 11,618 Points

You can keep the theme as it is. Just make your activity extend ActionBarActivity. Then in the layout file for your activity, change the id of the ListView to something like "@+id/listView".

Now you need to use the ListView like any other view in the activity by declaring a member variable for it and then assigning the ListView using findViewById(R.id.listView).

If you want to make stuff happen when an item in the list is clicked, then in the onCreate method add setOnItemClickListener() to the ListView variable.

If you run into any issues let me know, i'll write out the necessary code for you. But its better to attempt it yourself first, I believe.

Harley Privitera
Harley Privitera
13,316 Points

Thank You Ratul!

I just ran into this issue, and your suggestion worked perfectly. FWIW, I'm running Android Studio 1.2 Beta, so your fix is good at least up to that version.

ahmet yuva
ahmet yuva
17,595 Points

my code still gives me same error nullpointerexception even i did all what you wrote there

package com.ahmetyuva.ribbit;

import android.app.AlertDialog; import android.app.ListActivity; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView;

import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseQuery; import com.parse.ParseRelation; import com.parse.ParseUser;

import java.util.List;

public class RecipientsActivity extends ListActivity{

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

protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;

protected MenuItem mSendMenuItem;

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


    getActionBar().setDisplayHomeAsUpEnabled(true);




    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

@Override
public void onResume() {
    super.onResume();

    mCurrentUser = ParseUser.getCurrentUser();
    mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);


    ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
    query.addAscendingOrder(ParseConstants.KEY_USERNAME);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> friends, ParseException e) {

            if(e == null) {

                mFriends = friends;


                String[] usernames = new String[mFriends.size()];
                int i = 0;
                for (ParseUser user : mFriends) {
                    usernames[i] = user.getUsername();
                    i++;

                }                   ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getListView().getContext(),
                        android.R.layout.simple_list_item_checked,
                        usernames);
                setListAdapter(adapter);
            }
            else{

                Log.e(TAG, e.getMessage());
                AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);

                AlertDialog dialog = builder.create();
                dialog.show();

            }
        }
    });
}

@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_recipients, menu);
    mSendMenuItem = menu.getItem(0);
    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.
    switch (item.getItemId()){
        case android.R.id.home:

            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.action_send:
            return true;
    }
  /*  int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

*/ return super.onOptionsItemSelected(item); }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    if (l.getCheckedItemCount() > 0) {
        mSendMenuItem.setVisible(true);
    } else {
        mSendMenuItem.setVisible(false);
    }
}

}