【问题标题】:Resources returns a Drawable of the wrong type资源返回错误类型的 Drawable
【发布时间】:2014-03-25 05:52:00
【问题描述】:

我有一个包含三个ProgressBars 的应用程序,每个都有自己的自定义进度Drawable。这些 Drawable 相当简单——每个都是 LayerDrawable,带有一个透明背景层、一个透明的辅助进度层和一个纯色进度层。

所有三个ProgressBars 都以相同的布局显示。

我开始遇到的问题是其中一个ProgressBars 没有正确显示——它根本没有显示任何进度。这只发生在某些设备上(在运行 2.3.3 的模拟 Nexus One 和运行 4.1.2 的 Galaxy SII 上得到证实)。

我在onCreate 中设置了一个断点,发现前两个ProgressBarsmProgressDrawable 属性正确设置为LayerDrawable,但第三个设置为ColorDrawable

果然,下面的代码返回两个LayerDrawables和一个ColorDrawable

    Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
    Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
    Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);

无论我在布局和代码中将第三个ProgressBar 移动到何处,或者尝试交换progressDrawable 属性,引用我的第三个XML Drawable 的那个都没有显示任何进展并给我一个@987654338 @。

有趣的是,我发现只需在我的可绘制文件夹中创建一个新的 XML 文件即可解决问题。这让我相信 Android 打包或加载我的资源的方式存在问题,但我无法弄清楚如何识别和纠正根本问题。

我也无法在新应用程序中重现该问题。

我怎样才能继续追查这个问题的根源?

progressDrawableXML:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="@android:color/transparent" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/myapp_green" />
                <!-- The other two drawbles only change this color -->
            </shape>
        </clip>
    </item>

</layer-list>

colors.xml:

<resources>
    <color name="myapp_red">#dd514c</color>
    <color name="myapp_green">#5eb95e</color>
    <color name="myapp_blue">#0e90d2</color>
</resources>

移动 progressDrawable 属性的更新

  • 红、蓝、绿:3rd 坏了
  • 红、绿、绿:第 2 和第 3 坏了
  • 蓝、绿、红:第二个坏了
  • 蓝色、红色、蓝色:一切正常
  • 绿色,绿色,绿色:一切正常
  • 绿色、绿色、红色:一切正常
  • 绿色、蓝色、红色:一切正常

【问题讨论】:

  • 你能发布三个单独的可绘制 xml 文件吗?
  • 当然,他们是here
  • myapp_green 的定义是否有错字?
  • 我不这么认为。我已在我的帖子中添加了 colors.xml。根据我对下面答案的 cmets,当我将颜色从绿色切换到另一种颜色时,仍然会发生这种情况。
  • 重复的 progress_green.xml 是否可能存在于特定密度的可绘制文件夹之一中?

标签: android android-drawable


【解决方案1】:

如果您更改顺序,来自:

Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);
Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);

例如:

Drawable redDrawable   = getResources().getDrawable(R.drawable.progress_red);
Drawable greenDrawable = getResources().getDrawable(R.drawable.progress_green);
Drawable blueDrawable  = getResources().getDrawable(R.drawable.progress_blue);

问题仍然是greenDrawable还是第三个位置(blueDrawable)?

【讨论】:

  • 如果您将 更改为您在其他 progressDrawable 中使用的颜色会发生什么。还是错误?
  • 相同的结果 - ProgressBar 上没有颜色,它仍然显示为 ColorDrawable 而不是 LayerDrawable
  • ....我仍然没有解释...我认为唯一可能出错的是您的 xml 中我们看不到的东西...
  • 我当然也倾向于这个,但是这三个文件的差异表明只有颜色参考不同。
【解决方案2】:

如果您甚至没有看到进度条,可能是您在某处卡住了浅色背景?

尝试将样式属性设置为:

style="@android:style/Widget.ProgressBar.Inverse"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多