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 Relating Users in Parse.com Displaying Our List of Friends

My app crashes with an NullPointerException where I call getActionBar().setDisplayHomeAsUpEnabled(true);

Tried to google it, and all I've found was referring to ActionBarActivity.

2 Answers

We can't do much without your code included.

private static final String TAG = EditFriendActivity.class.getSimpleName(); protected List<ParseUser> mParseUsers; protected ParseRelation<ParseUser> mFriendsRelations; protected ParseUser mCurrentUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_friend);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

// getActionBar().setDisplayHomeAsUpEnabled(true); // At this point it would crash }

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

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

    setProgressBarIndeterminateVisibility(true);

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.orderByAscending(ParseConstants.KEY_USERNAME);
    query.setLimit(1000);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> parseUsers, ParseException e) {
            setProgressBarIndeterminateVisibility(true);
            if(e == null) {
                //Success
                mParseUsers = parseUsers;
                String[] usernames = new String[mParseUsers.size()];
                int i = 0;
                for (ParseUser user : mParseUsers) {
                    usernames[i] = user.getUsername();
                    i++;
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditFriendActivity.this, android.R.layout.simple_list_item_checked, usernames);
                setListAdapter(adapter);

                addFriendCheckmarks();
            } else {
                Log.e(TAG, e.getMessage());

                AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendActivity.this);
                builder.setTitle(getString(R.string.error_title));
                builder.setMessage(e.getMessage());
                builder.setPositiveButton(android.R.string.ok, null);
                builder.show();
            }

        }
    });
}

private void addFriendCheckmarks() {
    mFriendsRelations.getQuery().findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> parseUsers, ParseException e) {
            if(e == null) {
                //List returned
                for (int i = 0; i < mParseUsers.size(); i++) {
                    ParseUser user = mParseUsers.get(i);
                    for (ParseUser contact : parseUsers) {
                        if(contact.getObjectId().equals(user.getObjectId())) {
                            getListView().setItemChecked(i, true);
                        }
                    }
                }
            } else {
                Log.e(TAG, e.getMessage());
            }
        }
    });
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if(getListView().isItemChecked(position)){
        //Add friend
        mFriendsRelations.add(mParseUsers.get(position));
    } else {
        mFriendsRelations.remove(mParseUsers.get(position));
    }
    mCurrentUser.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {

// Log.e(TAG, e.getMessage()); } }); }

I doubt you'll get any help before learning how to post READABLE code in the comments. Use the search bar in the Forum section and see where it takes you!