Menuitemcompat.Getactionview Always Returns 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));

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);
    }

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);
}

(SearchView) MenuItemCompat.getActionView(item) always null

MenuItemCompat.getActionView(MenuItem item) is deprecated in API level 26.1.0.

Now the recommended way is calling getActionView() directly on the menu item:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();

// ...
}

Android MenuItemCompat.getActionView always returns null

Two problems:

  • You must use res-auto - the tool namespace set gets completely stripped before inclusion in your APK
  • Names are case sensitive - you need to use app:actionViewClass and app:showAsAction as per the Action Bar training

item.getActionview() always gives null

You have a typo it should be android.support not android:support

so use

<item
android:id="@+id/search"
android:title="Search"
android:icon="@mipmap/search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>

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