Android Studio编译错误并添加了资源 [英] Android Studio compile error with added resources

查看:70
本文介绍了Android Studio编译错误并添加了资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将以下代码行添加到我的程序中:

I have added this line of code to my program:

icon1.setImageResource(getResources().getIdentifier(item1, "drawable", getApplicationContext().getApplicationInfo().packageName));

并将图像(.png)添加到我的可绘制文件夹中.编译时出现以下错误:

And have added images (.png) to my drawable folder. When I compile I get the following error:

Error:(773, 32) error: <identifier> expected
Error:(773, 37) error: illegal start of type

R.java中的错误位置:

The error location in R.java:

public static final class drawable {
        public static final int 1001=0x7f020000;
        public static final int 1004=0x7f020001;
        public static final int 1006=0x7f020002;
        public static final int 1011=0x7f020003;
        public static final int 1018=0x7f020004;
        public static final int 1026=0x7f020005;
        public static final int 1027=0x7f020006;
        ..........

其中每个都会抛出一对自己的错误.

Each one of those throws its own pair of those errors.

我正在将图像放入/res/drawable-hdpi文件夹.文件名全都是数字(例如1023.png)的问题吗?

I'm putting the images into the /res/drawable-hdpi folder. Is the issue that the file names are all numeric (ex. 1023.png)?

这可能是什么原因?

推荐答案

像这样的行:

public static final int 1001=0x7f020000;

说声明类型为 int 的变量,其名称为 1001 ,其值为 0x7f020000 ".名称应为标识符(由字符 a - z A - Z _ ,只有在第一个字符之后才允许使用 0 - 9 .

are saying "declare a variable with type int whose name is 1001 and whose value is 0x7f020000". Names should be identifiers (consisting of the characters a-z, A-Z or _, with 0-9 being allowed only after the first character).

在这种情况下, 1001 是一个整数而不是一个标识符,这就是为什么会出现期望的"错误的原因.错误的类型的非法开始"指的是同一问题(编译器期望标识符提供类型的名称).

In this instance, 1001 is an integer not an identifier, which is why you get the " expected" error. The "illegal start of type" error is referring to the same issue (the compiler is expecting an identifier to provide the name of the type).

R.java 中的项目应为:

The items in R.java should be things like:

public static final int actionbar_logo=0x7f020000;

可绘制的标识符是根据文件名创建的,并且当您具有数字文件名时,它将使用这些值.您应该给图片起一个合理的名称,或在图片后加上前缀,例如 r1023 .

The drawable identifiers are created from the filenames and as you have numeric filenames it is using those values. You should give the images sensible names or append a prefix to them, e.g. r1023.

这篇关于Android Studio编译错误并添加了资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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