Android:获取abc_ic_ab_back_material时出现Resources$NotFoundException异常

10

在老版本(L之前)的设备上,我遇到了Resources$notfoundexception 异常。下面是完整的堆栈跟踪信息。

我的支持库版本是最新的(24.1.0),而且我已经在gradle文件中加入了以下代码:

vectorDrawables.useSupportLibrary = true

我使用的基础主题是一个无动作栏主题 - "Theme.AppCompat.Light.NoActionBar"。

我的代码中发生崩溃的位置在这一行,我在其中引用支持工具栏中的后退箭头,以便稍后更改其颜色:

@SuppressLint("PrivateResource") final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);

这可能是什么原因?该代码对于所有具有L或更高权限的用户都可以正常工作。

07-19 22:36:57.029 9330-9330/mypkg E/AndroidRuntime: FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to resume activity {mypkg/mypkg.activites.myActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
     at android.app.ActivityThread.access$600(ActivityThread.java:138)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:4929)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:511)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
     at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
     at android.content.res.Resources.loadDrawable(Resources.java:1957)
     at android.content.res.Resources.getDrawable(Resources.java:673)
     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
     at mypkg.base.mymethod(myactivity.java:100)
     at mypkg.mymethod(myactivity.java:100)
     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511)
     at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
     at android.app.Activity.performResume(Activity.java:5341)
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
     at android.app.ActivityThread.access$600(ActivityThread.java:138) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4929) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
     at dalvik.system.NativeStart.main(Native Method) 
  Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
     at android.content.res.Resources.loadDrawable(Resources.java:1954)
     at android.content.res.Resources.getDrawable(Resources.java:673) 
     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354) 
     at mypkg/mymethod(myactivity.java:100) 
     at mypkg/mymethod(myactivity.java:100) 
     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511) 
     at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178) 
     at android.app.Activity.performResume(Activity.java:5341) 
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599) 
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
     at android.app.ActivityThread.access$600(ActivityThread.java:138) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4929) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
     at dalvik.system.NativeStart.main(Native Method)
7个回答

24

4
我无法使它工作。我添加了你提到的那行代码,但应用程序仍崩溃。堆栈跟踪与你在问题中提到的一样。 - Artem Mostyaev
你是否也在 gradle 文件中添加了 useSupportLibrary 行,并且使用了最新的支持库版本? - Jon
1
是的。我猜问题是因为你的解决方案只适用于扩展AppCompatActivity的活动,而我的是PreferenceActivity。 - Artem Mostyaev
1
试着看看这个:https://dev59.com/CGMm5IYBdhLWcg3wIsQ2 - Jon
谢谢!使用AppCompatDelegate并没有解决崩溃问题,但是通过使用它,我已经添加了普通的工具栏,现在不需要图标了。 - Artem Mostyaev
此外,我不得不将我的xml可绘制对象从drawable-v21文件夹移动到drawable文件夹,以使它们对旧的API版本可见。在AppCompat 23.2.0之前的版本中,这是正确的方法,因为PNG图像会在构建时为旧的API版本创建。 - cybergen

2
请确保使用的是AppCompatActivity而不是Activity。如果您正在使用AppCompat的主题,则还需要使用它的Activity。

1

接受的答案未覆盖所有情况。它在Android 4.0.3/4.1.1/4.1.2平台上与25.x.x支持库不兼容。解决abc_ic_ab_back_material.xml问题的正确方法是在您的主题中覆盖homeAsUpIndicator属性。例如,我的主题继承自Theme.AppCompat.Light.NoActionBar。至于所提到的属性值,您可以使用@drawable/abc_ic_ab_back_mtrl_am_alpha或您自定义的“返回”可绘制对象。


1
另一种解决方案是,除了使用AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)之外,还可以将矢量图包装到另一个可绘制对象中:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_your_vector"/>
</selector>

当您将其用作TextView的drawable(即DrawableLeft)时,可能会很有用。


0

我在调用ContextCompat.getDrawable()时使用了应用程序上下文,这也导致应用程序崩溃并出现Resources$NotFoundException,尽管其他所有设置都已经完成,现在出现了以下消息:

如果您要使用的资源是矢量资源,则可能会以不受支持的方式引用它。有关更多信息,请参见AppCompatDelegate.setCompatVectorFromResourcesEnabled()。

我所要做的就是改为使用视图的Context :)


0

在编译时和运行时,私有资源会被不同地处理。为了解决这个问题,我通常会将有问题的私有资源回溯到我的应用程序中的 res 文件中。


谢谢。我尝试将资源复制到我的drawable文件夹中。它报告找不到该资源,所以我更改了名称以确保它不会尝试引用支持库版本 - 但它仍然报告找不到该资源。 - Jon
当你进行移植时,你必须更改所引用的命名空间,因为它不再是Android命名空间。 - Fred Grott

0

改为:

ContextCompat.getdrawable()

尝试使用:

AppCompatDrawableManager.get().getDrawable() 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接