如何修复资源未找到异常?

How to fix Resource not found exception?

我在 Android Studio (3.1.2) 中工作,我有一个应用程序最初是在 运行 InstantApp 管理器下创建的。 我相信这就是将其分为 3 个主要目录的原因:app、base、feature。

注意:突出显示相关文件...

无论如何,问题是找不到资源。

我正在尝试自定义 SeekBar 并使用渐变形状作为背景。在 feature.res.values.style.xml:

<?xml version="1.0" encoding="utf-8"?>

<style name="alphaSliderStyle" parent="@android:style/Widget.SeekBar">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:progressDrawable">@drawable/alpha_progress</item>
</style>

我的理解是 alpha_progress.xml 文件应该在 feature.res.drawable 下。但是,当 运行 应用程序实例化显示自定义 SeekBar 的视图时,我得到以下内容(摘录):

05-13 10:19:05.641 7962-7962/com.razorware.cloudcard.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.razorware.cloudcard.app, PID: 7962
android.view.InflateException: Binary XML file line #186: Binary XML file line #186: Error inflating class SeekBar
Caused by: android.view.InflateException: Binary XML file line #186: Error inflating class SeekBar
Caused by: android.content.res.Resources$NotFoundException: Drawable com.razorware.cloudcard.app:drawable/alpha_progress with resource ID #0x7f08005b
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/alpha_progress.xml from drawable resource ID #0x7f08005b
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:729)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:575)
    at android.content.res.Resources.loadDrawable(Resources.java:858)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
    at android.widget.ProgressBar.<init>(ProgressBar.java:290)
    at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:94)
    at android.widget.SeekBar.<init>(SeekBar.java:85)
    at android.widget.SeekBar.<init>(SeekBar.java:81)
    at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:47)
    at android.support.v7.widget.AppCompatSeekBar.<init>(AppCompatSeekBar.java:43)
    at android.support.v7.app.AppCompatViewInflater.createSeekBar(AppCompatViewInflater.java:238)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:149)
    at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
    at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
    at android.app.Dialog.setContentView(Dialog.java:532)
    at com.razorware.cloudcard.feature.visuals.ColorEditorDialog.initializeComponents(ColorEditorDialog.java:171)
    at com.razorware.cloudcard.feature.visuals.ColorEditorDialog.<init>(ColorEditorDialog.java:67)
    at com.razorware.cloudcard.feature.presentation.CloudCardPresenter.onClick(CloudCardPresenter.java:66)
    at android.view.View.performClick(View.java:5640)
    at android.view.View$PerformClick.run(View.java:22455)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6196)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
 Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <bitmap> requires a valid 'src' attribute
    at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:787)
    at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:747)
05-13 10:19:05.642 7962-7962/com.razorware.cloudcard.app E/AndroidRuntime:     at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1227)
        at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:285)
        at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:196)
        at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1227)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1200)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:719)
            ... 42 more

相关位似乎是:

Caused by: android.content.res.Resources$NotFoundException: Drawable com.razorware.cloudcard.app:drawable/alpha_progress with resource ID #0x7f08005b
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/alpha_progress.xml from drawable resource ID #0x7f08005b

...特别是它正在寻找 app.res.drawable 位置(如果我理解正确的话)。

关于它的价值,这里有另外两个相关的内容drawables

// alpha_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <bitmap android:src="@drawable/alpha_color_sample" />
    </item>
</layer-list>

// alpha_color_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<shape  xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:id="@+id/colorSample"
    android:layout_width="135dp"
    android:layout_height="45dp">

    <gradient android:angle="90"
        android:startColor="#FFFFFFFF"
        android:endColor="#00FFFFFF" />
</shape>
Caused by: android.content.res.Resources$NotFoundException: Drawable com.razorware.cloudcard.app:drawable/alpha_progress with resource ID #0x7f08005b
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/alpha_progress.xml from drawable resource ID #0x7f08005b

Resources.NotFoundException

This exception is thrown by the resource APIs when a requested resource can not be found.

您应该在 item 部分中设置 android:drawable

<item android:id="@android:id/background"
     android:drawable="@drawable/alpha_color_sample">
 </item>

我假设 <bitmap android:src 造成问题,因为 alpha_color_sample 不是有效的 位图.

修改后Clean-Rebuild-Run.