我有一个带有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)
答案 0 :(得分:0)
您可以直接为Activity调用findViewById()
,但是当您使用片段时,您需要一个视图对象来调用findViewById()
。例如。 getView().findViewById();
。
new PopupMenu(this,menuItemView);
new PopupMenu(this,menuItemView);
此处弹出菜单需要Context
,作为第一个参数传递。如果您正在使用活动,则可以使用this
,但在片段中,您需要使用getActivity()
代替this
只要您希望getActivity().getApplicationContext()
显示getApplicationContext()
,就可以使用Toast
而非Fragment
。
请使用以下代码。我希望你能得到解决方案:
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。