我想显示PopupMenu在recycerview里面,但是
当我单击menuButton应用时崩溃并发现问题时,我删除了从setOnClickListener调用的showPopupMenuMenu()方法并替换为Toast,它工作正常
问题出在showPopupMenuMenu()方法中的任何一个上,请帮助我解决此问题
holder.menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
showPopupMenuMenu(holder,position);
}catch (Exception e){
Log.e("showPopupMenuMenu",e.getMessage()+"");
}
}
});
private void showPopupMenuMenu(MyViewHolder holder, final int position){
//creating a popup menu
PopupMenu popup = new PopupMenu(context, holder.menuButton);
//inflating menu from xml resource
popup.inflate(R.menu.recyclerview_option_menu);
//adding click listener
try{
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit_menu:
//handle menu1 click
if (context instanceof MyAddressActivity) {
((MyAddressActivity) context).openEditAddressActivity(position, true);
} else if (context instanceof CheckoutLocationActivity) {
((CheckoutLocationActivity) context).openEditAddressActivity();
}
break;
case R.id.delete_menu:
//handle menu2 click
if (context instanceof MyAddressActivity) {
((MyAddressActivity) context).deleteAddressItem(position);
} else if (context instanceof CheckoutLocationActivity) {
((CheckoutLocationActivity) context).mCheckoutLocationActivityPresenter.deleteAddressItem(position);
}
break;
}
return false;
}
});
}catch (Exception e){
Log.e("showPopupMenuMenu","setOnMenuItemClickListener "+e.getMessage()+"");
}
//displaying the popup
popup.show();
}
二进制XML文件第32行:无法解析索引1处的属性:TypedValue {t = 0x2 / d = 0x7f0300cc a = -1}
R.menu.recyclerview_option_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/edit_menu"
android:title="@string/edit" />
<item
android:id="@+id/delete_menu"
android:title="@string/delete" />
</menu>
答案 0 :(得分:1)
如果您在Adapter中使用PopupMenu,请确保传递给Constructor的数据应该像
AddressAdapter addressAdapter=new AddressAdapter(AddressList.this,addressModels);
recyclerView.setAdapter(addressAdapter);
代替
AddressAdapter addressAdapter=new AddressAdapter(getApplicationContext(),addressModels);
recyclerView.setAdapter(addressAdapter);
注意:通过活动,而不是ApplicationContext()
答案 1 :(得分:0)
请尝试这个;
openPopUpMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getTitle().equals("1. item title")) {
//do something
} else if (menuItem.getTitle().equals("2.item Title")) {
// do something
}
return false;
}
});
}
});