回收站视图中的弹出菜单膨胀异常
PopUp Menu Inflate Exception in Recycler view
我有一个带有 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();
}
}
同一适配器从另一个 activity (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)
您可以直接为 Activity 调用 findViewById()
,但是由于您使用的是 Fragment,您将需要一个视图对象来调用 findViewById()
。例如。 getView().findViewById();
.
new PopupMenu(this, menuItemView);
new PopupMenu(this, menuItemView);
此处弹出菜单需要 Context
,作为第一个参数传递。如果你在 activity 中,你可以使用 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(); }
我有一个带有 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();
}
}
同一适配器从另一个 activity (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)
您可以直接为 Activity 调用 findViewById()
,但是由于您使用的是 Fragment,您将需要一个视图对象来调用 findViewById()
。例如。 getView().findViewById();
.
new PopupMenu(this, menuItemView);
new PopupMenu(this, menuItemView);
此处弹出菜单需要 Context
,作为第一个参数传递。如果你在 activity 中,你可以使用 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(); }