简体   繁体   中英

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.content.res.Resources$NotFoundException: File res/drawable/quickcontrolsprogress.xml from drawable resource 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.
at android.content.res.Resources.loadDrawable(Resources.java:3457)
at android.content.res.Resources.getDrawable(Resources.java:1897)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at 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)
...

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? Thank you!

if your Drawable has theme attribute

Try ContextCompat.getDrawable(getActivity(), R.drawable.quickcontrolsprogress);

or

if your Drawable has no theme attribute

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)

For API < 21 you can't use attributes to color in xml drawable. So only way is to use a reference to a color resource (@color/YOURS_COLOR) or use #RGB format.

worked for me: use activity context instead of application context

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

instead of:

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

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