二进制 XML 文件行 #32:无法解析索引 1 处的属性:TypedValue{t=0x2/d=0x7f0300cc a=-1}

Binary XML file line #32: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f0300cc a=-1}

我想显示 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>

请试试这个;

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

如果您在 Adapter 中使用 PopupMenu,请确保传递给 Constructor 的数据应该像

 AddressAdapter addressAdapter=new AddressAdapter(AddressList.this,addressModels);
                recyclerView.setAdapter(addressAdapter);

而不是

 AddressAdapter addressAdapter=new AddressAdapter(getApplicationContext(),addressModels);
                recyclerView.setAdapter(addressAdapter);

注意:传递 Activity“.this”而不是“ApplicationContext()”

您应该使用 导入 android.widget.PopupMenu; 库。 你不应该 androidx.appcompat.widget 这是行不通的。