I want to use popup to a menu item in action bar. It's everything ok when I use this in the Activity but when I switch to fragment I get:
MenuPopupHelper cannot be used without an anchor
my fragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_filter:
createCategoriesPopupMenu(item);
return true;
case R.id.action_sort:
createSortPopupMenu(item);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createSortPopupMenu(MenuItem item)
{
PopupMenu popup;
final View menuItemView = getView().findViewById(item.getItemId());
Log.wtf("d","item getitemid"+item.getItemId());
if (menuItemView == null)
{
Log.wtf("d","menuItemView==null view"+view.findViewById(item.getItemId()));
Log.wtf("d","menuItemView==null getAc"+getActivity().findViewById(item.getItemId()));
//final View searchItemView = getView().findViewById(R.id.action_search);
//popup = new PopupMenu(getActivity(), searchItemView);
}
popup = new PopupMenu(getActivity(), menuItemView);
Menu menu = popup.getMenu();
addSortMenuItems(menu);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
onSortMenuItemClick(item);
return true;
}
});
popup.show();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.issues_list_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
console output(assert):
07-08 14:08:16.659: A/d(1677): item getitemid2131165600 07-08
14:08:16.659: A/d(1677): menuItemView==null viewnull 07-08
14:08:16.663: A/d(1677): menuItemView==null getAcnull
I did't forgot setHasOptionsMenu(true); in onCreateView()
and view
is inflated layout from onCreateView()
Ok i found solution in xml menu file in item which we want to add popup we need to set
android:showAsAction="always"
becouse popup works only on icons in action bar not in menu. Also use getActivity().findView... not getView().findView...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With