选项菜单不适用于ThemeOverlay.AppCompat.Dark.ActionBar主题

时间:2017-09-13 05:39:53

标签: java android android-layout android-view android-theme

我创建了一个具有RecyclerView的应用程序,每个项目都有一个选项菜单,该菜单将显示要共享的弹出窗口。一切正常,但当我尝试点击选项菜单时,它会崩溃。我收到一个奇怪的错误。我谷歌它,似乎我的主题有问题。我在互联网上尝试了很多解决方案,但没有一个能为我工作。

这是我的主题:

<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>

这是我的Adapter类

 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>

我得到的错误是:

   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) 

有人能告诉我我做错了吗?

3 个答案:

答案 0 :(得分:0)

试试这个

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

我希望这会对你有所帮助。

答案 1 :(得分:0)

首先在适配器类中创建一个方法,如下所示。

 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();
 }

在你的 onBindViewHolder 方法中调用上面的函数,

holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        showPopup(view, news.get(holder.getAdapterPosition()).getUrl());
    }
});

答案 2 :(得分:0)

而不是:

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

执行:

PopupMenu popUp = new PopupMenu(view.getContext(), holder.buttonViewOption);