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.
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);
}
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 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
Masked Input Using Edittext Widget in Android
Transfer Data from One Activity to Another Activity Using Intents
Android:Understanding Drawable Folder
Volley Not Calling Getparams() for Standard Post Request
How to Update UI from Asynctask
How to Add "Android Design Support Library" to Eclipse with Adt-Plugin
How to Show Text in Android System Status Bar
Converting Preview Frame to Bitmap
Android - Build Separate APKs for Different Processor Architectures
How to Use Sharedpreferences to Save More Than One Values
Android - Way to Appear Bordered Text on the Textview
How to Update UI from Asynctask
App Getting Stuck with E/Com.Facebook.Internal.Attributionidentifiers
Handle a View Visibility Change Without Overriding the View
How to Find the Android Version Name Programmatically
Help Regarding Onclick() Event on an Item of Listview Custom Row Layout