TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Checking a Menu Item from PopUpMenu returns NullPointer

In my menu xml I have this:

<item
    android:id="@+id/action_payoff"
    android:visible="true"
    android:checkable="true"/>

In my adapter's getView() method, where each menu item has the same PopUpMenu, I do this:

    holder.ib.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            PopupMenu popup = new PopupMenu(getContext(), v);
            popup.getMenu().findItem(R.id.action_payoff).setChecked(true);

I get a NullPointerException. When I remove the setChecked(true); all works fine (of course my checkbox in my PopUpMenu item is not checked)

Does anyone have thoughts on this?

Sidenote: There is some logic that is not built in yet, of course. I was simply trying to access the menu item programatically before I connect it conditionally with a SharedPreference.

Upvotes: 2

Views: 432

Answers (1)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

I fixed it by moving the line of code AFTER the show() method, like so:

popup.show();                
popup.getMenu().findItem(R.id.action_include_payoff).setChecked(true);

Upvotes: 2

Related Questions