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.
Changing namespace of
actionViewClass
fromandroid:actionViewClass
toapp:actionViewClass
Implementing
android.support.v7.widget.SearchView.OnQueryTextListener
interface for current activity.Directly use
setOnQueryTextListener
instead ofSearchViewCompat.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
- thetool
namespace set gets completely stripped before inclusion in your APK - Names are case sensitive - you need to use
app:actionViewClass
andapp: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 extendActionBarActivity
instead ofFragmentActivity
directly - Each activity that uses the
ActionBarCompat
should declare its theme as inheriting from theActionBarCompat
themes.
Both of those I found watching the explanation video from Google.
Now my searchView
is not null
anymore.
Related Topics
How to Limit the Height of Spinner Drop Down View in Android
Getting the Battery Current Values for the Android Phone
Gradle Dsl Method Not Found: 'Compile()'
Add Button to a Layout Programmatically
Android Actionbar How to Add Supporting Library V7 Appcompat for Eclipse
"Canvas: Trying to Draw Too Large Bitmap" When Android N Display Size Set Larger Than Small
Android Intent for Playing Video
How to Use Radiogroup in Listview Custom Adapter
How to Get Android Device Screen Size
How to Change Focus Color of Edittext in Android
Make an Android Button Change Background on Click Through Xml
System.Exit(0) Doesnt Close All My Activities
How to Fix Error: No Signature of Method: Build_Ap86Oam3Dut3Pxce3X49Rdtma.Android()
Get JSON Data from Url Using Android