安卓资源未找到,因为宽度和高度 [英] Android resource not found because of width and height

查看:172
本文介绍了安卓资源未找到,因为宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发表在店里一个Android应用程序最近,非常完美,几乎我所有的用户。不过,我很快开始了一个星期获得了一些崩溃与以下跟踪报告:

I've published an Android app in the store recently that worked perfectly for almost all of my users. However, I shortly started getting a few crash reports a week with the following trace:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-xhdpi/dark_button_unpressed.png from drawable resource ID #0x7f02005d
at android.content.res.Resources.loadDrawable(Resources.java:1716)
at android.content.res.Resources.getDrawable(Resources.java:581)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1696)
... 40 more
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.content.res.Resources.loadDrawable(Resources.java:1711)
... 45 more

这是不容易确定究竟是怎么回事。我可以从用户那里搜集报告的唯一的事情是,它似乎大多发生在低密度显示器的设备。此特定资源被用作背景在XML的UI的图。

It is not easy to determine what exactly is going on here. The only thing I could gather from the user reports was that it seemed to mostly occur on devices with low density displays. This specific resource is used as background for a view in an XML UI.

推荐答案

下面是在Android的绘制资源和非常小的可绘制自动缩放的一个不幸的组合造成的。这个问题

The problem here is caused by an unfortunate combination of the automatic scaling of drawable resources in Android and very small drawables.

如果,例如,你的程序只提供了绘制-xhpdi 的资源,他们需要被缩减到较低的密度屏幕。这是由机器人自动完成的,如果你不提供这些资源的自己。

If, for example, your app only provides drawable-xhpdi resources, they need to be downsized to fit lower density screens. This is done automatically by Android if you don't provide these resources yourself.

显示密度的比例设置是这样的:

The scale of display densities is set to be like this:

  • xhdpi: 2.0
  • 华电国际: 1.5
  • MDPI: 1.0
  • LDPI: 0.75
  • xhdpi: 2.0
  • hdpi: 1.5
  • mdpi: 1.0
  • ldpi: 0.75

这意味着你的 xhdpi 资源是由上中密度显示因子2.0缩小。这可能是有害的质量,这也就是为什么它通常建议创建并提供这些较低密度资源自己。

That means that your xhdpi resources are scaled down by a factor 2.0 on medium density displays. This can be detrimental for the quality, which is why it is usually recommended to create and provide these lower density resources yourself.

现在,回到手头的问题。它不完全罕见有具有非常小的宽度或高度(1或2个像素)的一个资源。用例的一个例子是提供固体颜色背景的图。

Now, back to the problem at hand. It is not entirely uncommon to have a resource that has a very small width or height (1 or 2 pixels). An example of a use case is providing a solid color background for a view.

不幸的是,在提供这些资源 xhdpi 和缩放下来,像素大小可以被截断为0。有没有适当的控卫,这和Android将失败创建与维和崩溃的位图。

Unfortunately, when providing these resources as xhdpi and scaling them down, the pixel size can be truncated to 0. There is no guard in place for this and Android will fail to create a Bitmap with that dimension and crash.

有几种解决的:

  • 在包括资源为 LDPI ,并将它扩大相反,这并不影响目的作为背景。
  • 在包括资源为 nodpi 绘制,它永远不会被缩放。
  • 使用XML资源来代替。
  • Include the resource as ldpi and have it scale up instead, which doesn't affect the purpose as background.
  • Include the resource as nodpi drawable and it will never be scaled.
  • Use an XML resource instead.

最后一个选项最准确地描述了意图,并允许您在以后轻松改变颜色没有图像编辑程序:

The last option most accurately describes the intent and allows you to easily change the color later on without an image editing program:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#AARRGGBB" />
</shape>

包含这一资源在可绘制文件夹,它会一直正常工作。

Include this resource in the drawables folder and it will always work properly.

这篇关于安卓资源未找到,因为宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆