簡體   English   中英

使用 Android 4.4.4 找不到可繪制資源

[英]Drawable Resource not found using Android 4.4.4

我正在使用以下可繪制對象來裝飾我的進度條:

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

在 Android 6 或 7 上使用我的應用程序時,它工作正常,但如果我在 Kitkat 上嘗試它,則會引發以下錯誤:

android.content.res.Resources$NotFoundException:文件 res/drawable/quickcontrolsprogress.xml 來自可繪制資源 ID #0x7f020094。 如果您嘗試使用的資源是矢量資源,您可能會以不受支持的方式引用它。 有關詳細信息,請參閱 AppCompatDelegate.setCompatVectorFromResourcesEnabled()。
在 android.content.res.Resources.loadDrawable(Resources.java:3457)
在 android.content.res.Resources.getDrawable(Resources.java:1897)
在 android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
在 android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
在 android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
在 android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
在 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) 在 android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:990)
...

錯誤指向的代碼是:

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

有誰知道為什么會出現此 ResourceNotFound 異常以及如何解決? 謝謝!

如果您的Drawable具有主題屬性

嘗試使用ContextCompat.getDrawable(getActivity(), R.drawable.quickcontrolsprogress);

要么

如果您的Drawable沒有主題屬性

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

代替

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

這不是資源未找到的問題,錯誤是: UnsupportedOperationException :無法轉換為顏色:android.content.res.TypedArray.getColor(TypedArray.java:327)的類型= 0x2

對於API <21,您不能使用屬性在xml drawable中進行着色。 因此,唯一的方法是使用對顏色資源的引用(@ color / YOURS_COLOR)或使用#RGB格式。

為我工作:使用activity上下文而不是application上下文

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

代替:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM