简体   繁体   English

使用 Android 4.4.4 找不到可绘制资源

[英]Drawable Resource not found using Android 4.4.4

I'm costumizing my progressbar using the following drawable:我正在使用以下可绘制对象来装饰我的进度条:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <gradient
                android:startColor="@android:color/transparent"
                android:endColor="@android:color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                    android:startColor="@android:color/white"
                    android:endColor="@android:color/white" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="?attr/colorAccent"
                    android:endColor="?attr/colorAccent" />
            </shape>
        </clip>
    </item>
</layer-list>

When using my app on Android 6 or 7, it works fine but if I try it on Kitkat, the following error is thrown:在 Android 6 或 7 上使用我的应用程序时,它工作正常,但如果我在 Kitkat 上尝试它,则会引发以下错误:

android.content.res.Resources$NotFoundException: File res/drawable/quickcontrolsprogress.xml from drawable resource ID #0x7f020094. android.content.res.Resources$NotFoundException:文件 res/drawable/quickcontrolsprogress.xml 来自可绘制资源 ID #0x7f020094。 If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way.如果您尝试使用的资源是矢量资源,您可能会以不受支持的方式引用它。 See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info.有关详细信息,请参阅 AppCompatDelegate.setCompatVectorFromResourcesEnabled()。
at android.content.res.Resources.loadDrawable(Resources.java:3457)在 android.content.res.Resources.loadDrawable(Resources.java:3457)
at android.content.res.Resources.getDrawable(Resources.java:1897)在 android.content.res.Resources.getDrawable(Resources.java:1897)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)在 android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)在 android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)在 android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)在 android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at at.guger.musixs.fragment.ControlsFragment.onCreateView(ControlsFragment.java:86)在 at.guger.musixs.fragment.ControlsFragment.onCreateView(ControlsFragment.java:86)
... ...
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2 at android.content.res.TypedArray.getColor(TypedArray.java:327) at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:871) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990) Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2 at android.content.res.TypedArray.getColor(TypedArray.java:327) at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java :871) 在 android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
... ...

The code, the error is pointing at is:错误指向的代码是:

prgqProgress = (ProgressBar) rootView.findViewById(R.id.fr_ctrl_prgqProgress);
prgqProgress.setProgressDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.quickcontrolsprogress)); -> Line 86

Does anyone know why this ResourceNotFound Exception occurs and how to fix it?有谁知道为什么会出现此 ResourceNotFound 异常以及如何解决? Thank you!谢谢!

if your Drawable has theme attribute 如果您的Drawable具有主题属性

Try ContextCompat.getDrawable(getActivity(), R.drawable.quickcontrolsprogress); 尝试使用ContextCompat.getDrawable(getActivity(), R.drawable.quickcontrolsprogress);

or 要么

if your Drawable has no theme attribute 如果您的Drawable没有主题属性

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

instead of 代替

AppCompatResources.getDrawable(getContext(), R.drawable.quickcontrolsprogress)

It's not a resource not found problem, the error is: UnsupportedOperationException : Can't convert to color: type=0x2 at android.content.res.TypedArray.getColor(TypedArray.java:327) 这不是资源未找到的问题,错误是: UnsupportedOperationException :无法转换为颜色:android.content.res.TypedArray.getColor(TypedArray.java:327)的类型= 0x2

For API < 21 you can't use attributes to color in xml drawable. 对于API <21,您不能使用属性在xml drawable中进行着色。 So only way is to use a reference to a color resource (@color/YOURS_COLOR) or use #RGB format. 因此,唯一的方法是使用对颜色资源的引用(@ color / YOURS_COLOR)或使用#RGB格式。

worked for me: use activity context instead of application context为我工作:使用activity上下文而不是application上下文

ContextCompat.getDrawable(activity, R.drawable.v_lock)

instead of:代替:

ContextCompat.getDrawable(application, R.drawable.v_lock)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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