Java 颜色值可绘制资源问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20599262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:02:27  来源:igfitidea点击:

color value drawable resource issue

javaandroidresourcesdrawable

提问by ez4nick

I have an image that is 1900*1200 in a folder called drawable-sw600dp that id like to have used on a nexus 7. When I try to run the app the main activity the screen is white and I get the following error:

我在一个名为 drawable-sw600dp 的文件夹中有一个 1900*1200 的图像,我喜欢在 nexus 7 上使用它。当我尝试运行应用程序时,主活动屏幕是白色的,我收到以下错误:

java.lang.NumberFormatException: Color value '@drawable-sw600dp/background5' must start with #
at com.android.layoutlib.bridge.impl.ResourceHelper.getColor(ResourceHelper.java:71)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:248)
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:782)

Could it be that the image is too large to use? Or what could cause this error to happen?

会不会是图片太大无法使用?或者什么可能导致此错误发生?

This is in my activity_main.xml where the background is set:

这是在我的 activity_main.xml 中设置了背景:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background5"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/MainLayout"
tools:context=".MainActivity">

In my other folders drawable-hdpi,-mdpi there is no problem. However I made the .jpg image a higher resolution and now it creates this error, thats why I suspect the high resolution is causing the issue.

在我的其他文件夹 drawable-hdpi,-mdpi 中没有问题。但是,我使 .jpg 图像的分辨率更高,现在它产生了这个错误,这就是为什么我怀疑高分辨率是导致问题的原因。

回答by gahfy

As mentioned by android docjpg is not supported. Use png instead:

正如android doc所述,不支持 jpg。使用 png 代替:

A drawable to use as the background. This can be either a reference to a full drawable resource (such as a PNG image, 9-patch, XML state list description, etc), or a solid color such as "#ff000000" (black).

用作背景的可绘制对象。这可以是对完整可绘制资源(例如 PNG 图像、9-patch、XML 状态列表描述等)的引用,也可以是纯色,例如“#ff000000”(黑色)。

回答by Drew MacLean

Remove the '-' in the file name.

删除文件名中的“-”。

NOTE: '-' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

注意:“-”不是有效的基于文件的资源名称字符:基于文件的资源名称必须仅包含小写字母 az、0-9 或下划线

回答by Drew MacLean

Some people have mentioned restarting Eclipse has worked, but that didn't work for me.

有些人提到重新启动 Eclipse 有效,但这对我不起作用。

Instead, I found that I had to simply rename the image file so that it did NOT start with a number. All icons in the Android Developer Icon Pack start with a number, and I had just copied the one over. It appears that if the file starts with a number, it is assumed to be the start of a hexadecimal color value, and hence looks for the # symbol.

相反,我发现我必须简单地重命名图像文件,使其不以数字开头。Android Developer Icon Pack 中的所有图标都以数字开头,我刚刚复制了一个。看来,如果文件以数字开头,则假定它是十六进制颜色值的开头,因此会查找 # 符号。

working with: Eclipse Juno 3.7.2, Android SDK 21.1

使用:Eclipse Juno 3.7.2,Android SDK 21.1