Getactionview() of My Menuitem Return Null

MenuItemCompat.getActionView Returns Null

Try this

item.setActionView(R.layout.counter_action_bar_notification_icon);

final View menu_hotlist = MenuItemCompat.getActionView(item);
TextView ui_hot =(TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
ui_hot.setText(Integer.toString(13));

getActionView() of my MenuItem return null

getActionView() only works if there's a custom actionView from setActionView.

Why does getActionView() return null

In addition to the simple mistype (it should be menu_switch to match your XML), per the action view training, you have to use MenuItemCompat.getActionView() to extract the ActionView (and, in your case, cast it to SwitchCompat as there is no android.support.v7.widget.Switch).

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);

MenuItem item = menu.findItem(R.id.menu_switch);
switchButton = (SwitchCompat) MenuItemCompat.getActionView(item);

return super.onCreateOptionsMenu(menu);
}

MenuItemCompat.getActionView always returns null

Finally I found the solution.

  1. Changing namespace of actionViewClass from android:actionViewClass to app:actionViewClass

  2. Implementing android.support.v7.widget.SearchView.OnQueryTextListener interface for current activity.

  3. Directly use setOnQueryTextListener instead of SearchViewCompat.setOnQueryTextListener

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    if (searchView != null) {
    searchView.setOnQueryTextListener(this);
    }

    return super.onCreateOptionsMenu(menu);
    }

menuItem.getActionView() == null in API 15

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.

SearchView getActionView returning null

Today I had the same problem and I think I solved it. It turns out I did couple of things that were not exactly as per the ActionBarCompat contract:

  • Each activity that uses the ActionBarCompat should extend ActionBarActivity instead of FragmentActivity directly
  • Each activity that uses the ActionBarCompat should declare its theme as inheriting from the ActionBarCompat themes.

Both of those I found watching the explanation video from Google.

Now my searchView is not null anymore.



Related Topics



Leave a reply



Submit