I have a fragment with Recycler view
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);
Recycler view is inflated properly but I have a pop menu in each card of the recycler view.
But the popup menu binder is throwing a 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();
}
}
The same adapter when its called from another activity (Homeactivity) popup menu works fine but from the fragment(contained in welcomeactivity) it is throwing inflate exception.
AndroidManifest file
<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>
I expect the output to be something like when the menu button is clicked I see a crash
a
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)
you can call findViewById()
directly for Activity, however as you are using a Fragment, youo will need a view object to call findViewById()
. eg. getView().findViewById();
.
new PopupMenu(this, menuItemView);
new PopupMenu(this, menuItemView);
Here popup menu requires Context
, passed as first parameter. You can use this
if you are in activity, however in Fragment you need to use getActivity()
instead of this
Use getActivity().getApplicationContext()
instead of just getApplicationContext()
whenever you want to display Toast
from Fragment
.
Please use below code. I hope you get your solution:
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(); }
For more detail please Check this tutorial.