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


Eric De Wildt
Eric De Wildt
Courses Plus Student 13,077 Points

Toolbar overflow menu context

I have an overflow menu inside a Toolbar on a CardView and need to pass the proper context into the menu. Can someone have a look at my code and help me understand?

public class ReturnVisitsAdapter extends RealmRecyclerViewAdapter<ReturnVisit> {

    private class ReturnViewHolder extends RecyclerView.ViewHolder {

        public android.support.v7.widget.Toolbar mCardViewToolbar;
        public TextView mEmail;
        public TextView mAddress;
        public TextView mPhone;

        public ReturnViewHolder(View view) {

            mCardViewToolbar = (android.support.v7.widget.Toolbar) view.findViewById(R.id.textViewRecyclerCardViewToolbar);
            mEmail = (TextView) view.findViewById(R.id.textViewRecyclerCardViewEmail);
            mAddress = (TextView) view.findViewById(R.id.textViewRecyclerCardViewAddress);
            mPhone = (TextView) view.findViewById(R.id.textViewRecyclerCardViewPhone);

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.returnvisit_card_view, parent, false);
        return new ReturnViewHolder(view);

    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int i) {
        final ReturnViewHolder returnViewHolder = (ReturnViewHolder) viewHolder;
        final ReturnVisit returnVisit = getItem(i);

        if (returnViewHolder.mCardViewToolbar.getMenu().size() == 0) {
            returnViewHolder.mCardViewToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem menuItem) {
//                     Handle item selection
                    switch (menuItem.getItemId()) {
                        case R.id.menuCardViewCall:
//                        callNumber();
                            return true;
                        case R.id.menuCardViewMessage:
//                        sendTextWithDefaultTextApp();
                            return true;
                        case R.id.menuCardViewEmail:
//                        sendEmailWithEmailApp();
                            return true;
                        case R.id.menuCardViewDelete:
//                            TODO find out how to get context into this menu.
//                            Realm realm = Realm.getInstance(CONTEXT GOES HERE);
//                            realm.executeTransaction(new Realm.Transaction() {
//                                @Override
//                                public void execute(Realm realm) {
//                                    returnVisit.removeFromRealm();
//                                }
//                            });


                            return true;
                            return false;

    /* The inner RealmBaseAdapter
     * view count is applied here.
     * getRealmAdapter is defined in RealmRecyclerViewAdapter.
    public int getItemCount() {
        if (getRealmAdapter() != null) {
            return getRealmAdapter().getCount();
        return 0;

I keep getting null object reference on the context.

1 Answer

Georgi Koemdzhiev
Georgi Koemdzhiev
12,610 Points


You probably got your answer but here's why I thing your are getting null on the context. You normally get a reference to the context in the adapter's constructer as well as a reference to the data set (for example a list or arraylist). After that you store it in a local variable (mActivity) and this is what you pass to the inflate method not parent.getContext() :)