[英]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 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 在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 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.
我试过将anchorview切换到imagebutton的父级,但这也没有用。 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. 编辑:cox_background.xml也是res文件夹中的可绘制对象。 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. Edit2:cox_background.xml在res / drawable文件夹中。 Testing on Google Nexus 5X running Marmallow.
在运行Marmallow的Google Nexus 5X上进行测试。
Edit3: call_page1.png is in drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi folders. Edit3:call_page1.png位于drawable-hdpi,drawable-mdpi,drawable-xhdpi,drawable-xxhdpi,drawable-xxxhdpi文件夹中。 drawable-hdpi/call_page1.png
可绘制-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).
在styles.xml中,我定义了供整个应用程序遵循的样式(我主要使用主题编辑器制作了样式)。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.