【问题标题】:PopUp Menu Inflate Exception in Recycler view回收站视图中的弹出菜单膨胀异常
【发布时间】:2023-03-28 23:46:01
【问题描述】:

我有一个带有 Recycler 视图的片段

View view = inflater.inflate(R.layout.recyclerview, container, false);
ViewGroup fragmentview=(ViewGroup)getView();
    dashboardcontentList = new ArrayList<>();
    adapter = new DashboardAdapter(getActivity().getApplicationContext(), dashboardcontentList);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

回收站视图已正确膨胀,但我在回收站视图的每张卡片中都有一个弹出菜单。

但是弹出菜单绑定器抛出了 infateException

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    dashboardcontent dashboardcontent = dashboardcontentList.get(position);
    holder.title.setText(dashboardcontent.getName());
    Glide.with(mContext).load(dashboardcontent.getThumbnail()).into(holder.thumbnail);
    holder.overflow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showPopupMenu(holder.overflow);
        }
    });
}

/**


 * Showing popup menu when tapping on 3 dots
     */
    private void showPopupMenu(View view) {
        // inflate menu
        PopupMenu popup = new PopupMenu(mContext, view);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu_album, popup.getMenu());
        popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
        try{
        popup.show();}
        catch (Exception e){
            e.printStackTrace();
        }
    }

当从另一个活动(Homeactivity)弹出菜单调用时,同一个适配器工作正常,但从片段(包含在welcomeactivity)中,它会引发膨胀异常。

AndroidManifest 文件

<application
        android:name=".medipal.App"
        android:allowBackup="true"
        android:debuggable="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.HomeActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.Welcome"
            android:label="@string/title_activity_welcome"
            android:theme="@style/AppTheme"              android:parentActivityName=".activity.HomeActivity" />
        <activity android:name=".activity.HelpScreen" />
        <activity android:name=".activity.Recyclerview"
            android:label="Recyclerview"
            android:theme="@style/AppTheme">
        </activity>
        <activity android:name=".activity.contacts">


</manifest>

我希望输出类似于 when the menu button is clicked I see a crash

一个

ndroid.view.InflateException: Binary XML file line #17: Failed to resolve attribute at index 6: TypedValue{t=0x3/d=0x379 "res/drawable/ic_menu_moreoverflow_material.xml" a=1 r=0x10803d9}
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                       at android.support.v7.view.menu.MenuAdapter.getView(MenuAdapter.java:93)
                                                       at android.support.v7.view.menu.MenuPopup.measureIndividualMenuWidth(MenuPopup.java:160)
                                                       at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:153)
                                                       at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
                                                       at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
                                                       at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
                                                       at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:141)
                                                       at android.support.v7.widget.PopupMenu.show(PopupMenu.java:233)

【问题讨论】:

  • 请检查我的更新答案..

标签: android android-fragments android-recyclerview popupmenu inflate-exception


【解决方案1】:

您可以直接为 Activity 调用 findViewById(),但是当您使用 Fragment 时,您将需要一个视图对象来调用 findViewById()。例如。 getView().findViewById();.

  1. View - findViewById()
  2. Activity - findViewById()

new PopupMenu(this, menuItemView);

new PopupMenu(this, menuItemView);

这里的弹出菜单需要Context,作为第一个参数传递。如果你在活动中,你可以使用this,但是在Fragment中你需要使用getActivity()而不是this

当您想从Fragment 显示Toast 时,请使用getActivity().getApplicationContext() 而不仅仅是getApplicationContext()

请使用以下代码。我希望你能得到你的解决方案:

public void popup_window() {

View menuItemView = getView().findViewById(R.id.menu_popup); PopupMenu popupMenu = new PopupMenu(getActivity(), menuItemView); popupMenu.getMenuInflater().inflate(R.menu.list_, popupMenu.getMenu());

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {  
            case R.id.action_ticket:  
                Intent intdn = new Intent(getActivity(),List_Activity.class); // Your nxt activity name instead of List_Activity
                intdn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);                   
                getActivity().startActivity(intdn);
              break;    

            case R.id.action_survey:  
                Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;      
            case R.id.action_service_request:  
                Toast.makeText(getActivity().getApplicationContext(),"Under Construction ",Toast.LENGTH_LONG).show();  
                break;  

              default: 
                  break;  

        }  
         return true;
    } }); popupMenu.show(); }

更多详情请Check this tutorial

【讨论】:

  • 试过了,我也在做同样的事情,但代码在 popup.show() 处仍然中断。当我从另一个活动中调用时,我从片段中调用它时问题不存在。
猜你喜欢
  • 2016-01-27
  • 2020-04-04
  • 2016-09-29
  • 2017-02-19
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多