coder
coder

Reputation: 10520

menuItem.getActionView() == null in API 15

I'm supporting down to API 15 in my app, and I'm getting a crash for those users when I try to get the searchView from my menu.

Below is my code:

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    MenuItem menuItem = menu.getItem(0);
    searchView = (SearchView)menuItem.getActionView();
    searchView.setIconifiedByDefault(false);
    searchView.setQuery("", true);
    menuItem.expandActionView();
}

I'm getting a NullPointerException on this line:

searchView.setIconifiedByDefault(false);

because the searchView is null. This works perfectly fine on devices at API 16 and above. Has anyone run into this issue before?

Upvotes: 1

Views: 808

Answers (2)

hata
hata

Reputation: 12478

I also had just the same problem as you and came to this stackoverflow question. With some struggle, I have found the heart of this problem and a solution.

In API15, during app's initialization, only onCreateOptionsMenu is called but onPrepareOptionsMenu is not.

In API16 and later, onPrepareOptionsMenu is called right after onCreateOptionsMenu.

So my solution is to call onPrepareOptionsMenu at the ending point of onCreateOptionsMenu:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    (...)

    if (Build.VERSION.SDK_INT < 16) {
        onPrepareOptionsMenu(menu);
    }
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006664

While inflating a layout usually causes an immediate crash if there is a problem, inflating a menu resource does not. If there is some problem, a stack trace is logged, but otherwise the exception is handled, and so execution continues. It is only some time later that we realize that something did not work, when things break later on.

Custom action bar items (actionLayout, actionViewClass, actionProvider) are especially prone to this. If there is some problem loading any of those -- such as the actionViewClass not implementing the proper constructor -- we only find out about it when we try to retrieve the custom item and get null back. The solution is to rummage through LogCat and look for the stack trace associated with the handled exception, to see what really went wrong.

In an API level-dependent case, like this one, the most likely scenario would be where initialization of custom action item refers to a method that does not exist on the older version of Android, and therefore fails.

Upvotes: 1

Related Questions