Android popupmenu崩溃应用程序资源未找到异常

时间:2016-02-18 03:52:55

标签: java android xml popupmenu

所以我收到以下错误:

android.content.res.Resources$NotFoundException: File res/drawable/cox_background.xml from color state list resource ID #0x7f02004d
                                                                           at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2750)
                                                                           at android.content.res.Resources.loadColorStateList(Resources.java:2699)
                                                                           at android.content.res.TypedArray.getColorStateList(TypedArray.java:485)
                                                                           at android.graphics.drawable.GradientDrawable.updateGradientDrawableSolid(GradientDrawable.java:1320)
                                                                           at android.graphics.drawable.GradientDrawable.applyThemeChildElements(GradientDrawable.java:1148)
                                                                           at android.graphics.drawable.GradientDrawable.applyTheme(GradientDrawable.java:1056)
                                                                           at android.content.res.Resources.loadDrawable(Resources.java:2549)
                                                                           at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                                                                           at android.widget.PopupWindow.<init>(PopupWindow.java:224)
                                                                           at android.widget.PopupWindow.<init>(PopupWindow.java:210)
                                                                           at android.support.v7.widget.AppCompatPopupWindow.<init>(AppCompatPopupWindow.java:44)
                                                                           at android.support.v7.widget.ListPopupWindow.<init>(ListPopupWindow.java:251)
                                                                           at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:139)
                                                                           at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129)
                                                                           at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216)
                                                                           at com.coxauto.forcedroidapp.MainActivity$2.onClick(MainActivity.java:166)
                                                                           at android.view.View.performClick(View.java:5204)
                                                                           at android.view.View$PerformClick.run(View.java:21153)
                                                                           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:5417)
                                                                           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)
                                                                        Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid color state list tag shape
                                                                           at android.content.res.ColorStateList.createFromXmlInner(ColorStateList.java:217)
                                                                           at android.content.res.ColorStateList.createFromXml(ColorStateList.java:201)
                                                                           at android.content.res.Resources.loadColorStateListForCookie(Resources.java:2746)
                                                                           at android.content.res.Resources.loadColorStateList(Resources.java:2699) 
                                                                           at android.content.res.TypedArray.getColorStateList(TypedArray.java:485) 
                                                                           at android.graphics.drawable.GradientDrawable.updateGradientDrawableSolid(GradientDrawable.java:1320) 
                                                                           at android.graphics.drawable.GradientDrawable.applyThemeChildElements(GradientDrawable.java:1148) 
                                                                           at android.graphics.drawable.GradientDrawable.applyTheme(GradientDrawable.java:1056) 
                                                                           at android.content.res.Resources.loadDrawable(Resources.java:2549) 
                                                                           at android.content.res.TypedArray.getDrawable(TypedArray.java:870) 
                                                                           at android.widget.PopupWindow.<init>(PopupWindow.java:224) 
                                                                           at android.widget.PopupWindow.<init>(PopupWindow.java:210) 
                                                                           at android.support.v7.widget.AppCompatPopupWindow.<init>(AppCompatPopupWindow.java:44) 
                                                                           at android.support.v7.widget.ListPopupWindow.<init>(ListPopupWindow.java:251) 
                                                                           at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:139) 
                                                                           at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129) 
                                                                           at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216) 
                                                                           at com.coxauto.forcedroidapp.MainActivity$2.onClick(MainActivity.java:166) 
                                                                           at android.view.View.performClick(View.java:5204) 
                                                                           at android.view.View$PerformClick.run(View.java:21153) 
                                                                           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:5417) 
                                                                           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) 

对于我的生活,我无法弄清楚,在查看文档和查看其他文章之间。当用户单击屏幕顶部的图像按钮时会发生这种情况

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cox_background">

<RelativeLayout
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <com.coxauto.forcedroidapp.CoxTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        app:fontName="HelveticaNeueLTStd-LtCn.otf"/>

    <ImageButton
        android:id="@+id/call"
        android:layout_width="@dimen/call_size"
        android:layout_height="@dimen/call_size"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="?android:attr/selectableItemBackground"
        android:layout_marginRight="10dp"
        android:onClick="showPopUp"/>

</RelativeLayout>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/bar"
    android:layout_alignParentBottom="true"/>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="20dp"
    android:src="@android:drawable/ic_dialog_email"
    app:backgroundTint="@color/mustard"
    android:onClick="onFabClick"/>

</RelativeLayout>

在MainActivity.java

中加载页面后调用此方法
call = (ImageButton) findViewById(R.id.call);

    Picasso
            .with(this)
            .load(R.drawable.call_page1)
            .fit()
            .into(call);

    call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout appBar = (RelativeLayout)MainActivity.this.findViewById(R.id.bar);
            PopupMenu popup = new PopupMenu(MainActivity.this, v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.contacts_menu, popup.getMenu());

            popup.setOnMenuItemClickListener(MainActivity.this);

            popup.show();
        }
    });

contacts_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:icon="@drawable/call_page1">

<item
    android:id="@+id/sales_rep"
    android:title="@string/sales_rep">

</item>

<item
    android:id="@+id/rewards_cons"
    android:title="@string/rewards_cons">

</item>
</menu>

非常感谢任何帮助。这应该是一件简单的事情,但出于某种原因给我带来了很多麻烦。我已经尝试将anchorview切换到图像按钮的父级,但这也没有用。到目前为止,我有一种预感,这是布局的问题。

编辑:cox_background.xml也是res文件夹中的drawable。这是以防万一。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<gradient
    android:startColor="#003469"
    android:centerColor="#003C72"
    android:endColor="#0585c8"
    android:angle="315"/>
</shape>

Edit2:cox_background.xml位于res / drawable文件夹中。在运行Marmallow的Google Nexus 5X上进行测试。

Edit3:call_page1.png在drawable-hdpi,drawable-mdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxxhdpi文件夹中。 drawable-hdpi/call_page1.png

这些是透明图像。

1 个答案:

答案 0 :(得分:0)

所以我在这里发现了问题。在styles.xml中,我为整个应用程序定义了一个样式(我主要使用主题编辑器制作样式)。我为背景变量分配了一个drawable。直到现在我认为应用程序尝试使用菜单时,这并没有给我带来任何问题。

<style name="CoxTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@android:color/transparent</item>
    <!-- This cannot be a drawable. Must be a color-->
    <item name="android:colorBackground">@drawable/cox_background</item>
    <item name="android:textColorPrimary">@color/cox_text_primary</item>
    <item    name="android:textColorSecondary">@color/secondary_text_material_light</item>
    <item name="android:textColorPrimaryInverse">@color/primary_text_material_dark</item>
    <item name="android:screenOrientation">portrait</item>
    <item name="colorAccent">@color/cox_accent</item>
</style>