Option menu not working with theme ThemeOverlay.AppCompat.Dark.ActionBar

0 votes
asked Sep 13, 2017 by gaurav-tiwari

I have created an application which has a RecyclerView and each item has an option menu, which will show the popup to share. Everything is working fine but when I try to click on the option menu it crashes. I am getting a weird error. I google it and it seems like there is something wrong with my theme. I tried many solutions available on the internet but none of them working for me.

Here is my theme:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="android:textColorPrimary">#ffffff</item> <item name="android:textColorSecondary">@color/about_us_link_text_color</item>
</style>

This is my Adapter class

 holder.buttonViewOption.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { PopupMenu popUp = new PopupMenu(context,holder.buttonViewOption); popUp.inflate(R.menu.option_menu); popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.share: Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT,news.get(position).getUrl()); intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site"); context.startActivity(Intent.createChooser(intent,"share")); break; case R.id.save: break; } return false; } }); popUp.show(); } });

option_menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/share" android:title="Share" /> <item android:id="@+id/save" android:title="Save" />
</menu>

The error I am getting is :

 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x1d/d=0xffff4081 a=3 r=0x7f0c001a} at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:705) at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6890) at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7071) at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:446) at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:386) at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:385) at android.view.LayoutInflater.inflate(LayoutInflater.java:502) 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)  at com.kotlin.whatshappening.activity.adapter.NewsAdapter$2.onClick(NewsAdapter.java:142)  at android.view.View.performClick(View.java:5204)  at android.view.View$PerformClick.run(View.java:21158)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5461)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Can someone tell me what I am doing wrong?

3 Answers

0 votes
answered Sep 13, 2017 by azizbekian

Instead of:

PopupMenu popUp = new PopupMenu(context, holder.buttonViewOption);

Perform:

PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);
0 votes
answered Sep 13, 2017 by zephyr

Try this

popUp.getMenuInflater().inflate(R.menu.option_menu, popUp.getMenu()); 

I hope this helps you.

0 votes
answered Mar 9, 2018 by kcg

First create a method in you adapter class like the following.

 private void showPopup(View view, String url) { PopupMenu popup = new PopupMenu(activityContext, view); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.option_menu, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.share: Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT,url); intent.putExtra(Intent.EXTRA_SUBJECT,"check out this site"); activityContext.startActivity(Intent.createChooser(intent,"share")); break; case R.id.save: break; } return true; } }); popup.show(); }

And in your onBindViewHolder method call the above function,

holder.buttonViewOption.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showPopup(view, news.get(holder.getAdapterPosition()).getUrl()); }
});
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...