简体   繁体   中英

Android popupmenu crashes app with resource not found exception

So I'm getting the following error:

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) 

And for the life of me I can't figure it out, between looking at the documentation and looking at other articles. This occurs when the user clicks the imagebutton at the top of the screen

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>

This is called after the page is loaded in 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>

Any help would be greatly appreciated. This should be a simple thing but for some reason is giving me a lot of trouble. I've tried switching the anchorview to the parent of the imagebutton but that also didn't work. So far I have a hunch that it's a problem with the layout.

Edit: also cox_background.xml is a drawable in the res folder. Here it is just in case.

<?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 is in the res/drawable folder. Testing on Google Nexus 5X running Marmallow.

Edit3: call_page1.png is in drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi folders. drawable-hdpi/call_page1.png

These are transparent images.

So I found the problem here. In styles.xml I defined a style for the entire app to follow (I mostly made the style with theme editor). I assigned a drawable to the background variable. This didn't give me problems until now I suppose when the app tries to use menus.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM