在开发Hello,Gallery tutorial/sample应用程序时,在站点上执行following the instructions之后,Eclipse报告R.styleable无法解析。
此错误的原因是什么?如何修复或解决此错误?
发布于 2009-11-12 03:36:35
根据this thread,R.styleable已经从Android1.5和更高版本中删除。
有许多方法可以让样本工作,我找到的最简单的方法是Justin Anderson在上面链接的帖子中推荐的:
values创建一个新的名为"resources.xml“的XML文件,其中包含以下内容:
c.obtainStyledAttributes(R.styleable.Gallery1);ImageAdapter(ImageAdapter c) { mContext = c;TypedArray a= public mGalleryItemBackground =public 0);a.recycle();}
该解决方案基本上将styleable属性定义为应用程序本身的资源,并为其提供在应用程序中工作所需的结构。请注意,如果您只需省略这两行代码(在a.recycle();之前),应用程序就可以正常运行,所有这些代码所做的就是在Gallery中的图像周围设置一个灰色背景。
发布于 2010-05-06 15:41:37
出现此问题的原因是它们告诉您要放入res/values/attrs.xml的资源是:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>但是你得到了这个适配器,Eclipse找不到它,坦率地说,它没有任何意义:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
android.R.styleable.Theme_galleryItemBackground, 0);
a.recycle();
}那是因为你不应该有“机器人”。在资源之前,可设置样式的名称在这里是主题,但在实际的资源中是HelloGallery,galleryItemBackground将android放在可设置样式的名称和属性之间,如下所示: Theme_android_galleryItemBackground
因此,如果想让ImageAdapter方法与给定的资源一起工作,您应该像这样重写它:
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}对于将来有关资源的问题(R.*无法解决类型错误),请检查/gen/R.java以了解资源实际命名的内容。
发布于 2014-06-08 16:36:13
我也有同样的问题,我在谷歌的自定义视图示例代码(PieChart)中发现了这个问题
import com.example.android.customviews.R;当我注释导入行时,Eclipse将注意到错误:"R无法解析为变量“。所以你应该用类似于上面的语句来导入你的包。例如:
import your.package.name.R;它为我的其他项目修复了类似的错误
https://stackoverflow.com/questions/1717489
复制相似问题