Android Hello, Gallery 教程——“R.styleable 无法解析”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1717489/
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-20 03:30:56  来源:igfitidea点击:

Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"

androideclipse

提问by Guy Starbuck

When working on the Hello, Gallery tutorial/sample app, after following the instructionson the site, Eclipse reported that R.styleable cannot be resolved.

在处理 Hello, Gallery 教程/示例应用程序时,按照站点上的说明进行操作后,Eclipse 报告无法解析 R.styleable。

What is the reason for this error, and how can it be fixed or worked around?

此错误的原因是什么,如何修复或解决?

回答by Guy Starbuck

Per this thread, R.styleable has been removed from android 1.5 and higher.

根据此线程,R.styleable 已从 android 1.5 及更高版本中删除。

There are a number of ways to get the sample to work, the simplest that I found was recommended by Justin Anderson in the thread linked to above:

有多种方法可以使示例工作,我发现最简单的方法是 Justin Anderson 在上面链接的线程中推荐的:

  1. Create a new XML file called "resources.xml" with the following content:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="Gallery1"> 
            <attr name="android:galleryItemBackground" /> 
        </declare-styleable> 
    </resources>
    
  2. Place the XML file in the res\values directory (alongside strings.xml)

  3. Update the constructor for your ImageAdapter with the following (assuming the ImageAdapter class is defined in its own file):

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }
    
  1. 使用以下内容创建一个名为“resources.xml”的新 XML 文件:

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="Gallery1"> 
            <attr name="android:galleryItemBackground" /> 
        </declare-styleable> 
    </resources>
    
  2. 将 XML 文件放在 res\values 目录中(与 strings.xml 一起)

  3. 使用以下内容更新 ImageAdapter 的构造函数(假设 ImageAdapter 类在其自己的文件中定义):

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();
    }
    

This solution basically defines the styleable attribute as a resource of the app itself and gives it the necessary structure to work in the app. Note that the app can run fine if you just omit the two lines of code (prior to a.recycle();), all this code does is set a grey background around the images in the Gallery.

该解决方案基本上将 styleable 属性定义为应用程序本身的资源,并为其提供在应用程序中工作的必要结构。请注意,如果您只是省略两行代码(在 a.recycle(); 之前),该应用程序可以正常运行,所有这些代码所做的就是在图库中的图像周围设置灰色背景。

回答by Sven

The reason for this problem is the resources they tell you to put into res/values/attrs.xml are:

这个问题的原因是他们告诉你放入 res/values/attrs.xml 的资源是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="HelloGallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>

But then you get this adapter, which Eclipse can't figure out and frankly makes no sense:

但是随后您得到了这个适配器,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();
}

That's because you shouldn't have "android." preceeding the resources, the styleable name is Theme here but HelloGallery in the actual resource, and the galleryItemBackground puts android between the styleable name and the attribute like this: Theme_android_galleryItemBackground

那是因为你不应该有“android”。在资源之前,这里的样式名称是 Theme 而实际资源中的 HelloGallery,并且 galleryItemBackground 将 android 放在样式名称和属性之间,如下所示:Theme_android_galleryItemBackground

So if want the ImageAdapter method to work with the resources you're given, you should rewrite it like this:

因此,如果希望 ImageAdapter 方法使用您提供的资源,您应该像这样重写它:

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = a.getResourceId(
            R.styleable.HelloGallery_android_galleryItemBackground, 0);
    a.recycle();
}

For future problems regarding resources (R.* cannot be resolved type errors), examine /gen/R.java for what the resources are actually being named.

对于有关资源的未来问题(R.* 无法解决类型错误),请检查 /gen/R.java 以了解资源的实际名称。

回答by Steve Pomeroy

A slightly easier, and certainly more MVCish way is to use the style system:

一种更简单,当然也更符合 MVC 风格的方法是使用样式系统:

If you don't have a theme yet, create a styles.xmlunder res/values. In it, you should have:

如果您还没有主题,请styles.xmlres/values. 在里面,你应该有:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="GalleryItem">
        <item name="android:background">?android:attr/galleryItemBackground</item>
    </style>
</resources>

This will define a new style which we are calling GalleryItemand setting the background resource of whatever the style gets applied to, to the value of the style attribute android:attr/galleryItemBackground(you can see a lot of examples of this being done in the frameworks/base/core/res/res/values/themes.xmlin Android's source).

这将定义一个我们正在调用的新样式,GalleryItem并将该样式应用到的任何背景资源设置为 style 属性的值android:attr/galleryItemBackground(您可以frameworks/base/core/res/res/values/themes.xml在 Android 的源代码中看到很多有关此操作的示例)。

Then in an XML declaration for an ImageView, you can simply apply your GalleryItemstyle by adding style="@style/GalleryItem", eg:

然后在 ImageView 的 XML 声明中,您可以GalleryItem通过添加简单地应用您的样式style="@style/GalleryItem",例如:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/icon"
  android:scaleType="fitXY"
  android:layout_width="136dip"
  android:layout_height="88dip"
  style="@style/GalleryItem"
/>

This will keep your style stuff out of your adapter code (which is good!) and allow for more generic adapters that don't need to care how you're visualizing your data.

这将使您的样式内容远离您的适配器代码(这很好!)并允许使用更通用的适配器,而无需关心您如何可视化数据。

回答by Dai Nguyen-Van

I have the same problem and I found in the custom view sample code (PieChart) of Google

我有同样的问题,我在谷歌的自定义视图示例代码(PieChart)中找到

import com.example.android.customviews.R;

when i comment that import line, Eclipse will notice error: "R cannot to be resolved to a variable". so you should make an import of your package similar statement above. Ex:

当我注释该导入行时,Eclipse 会注意到错误:“R 无法解析为变量”。所以你应该像上面的声明一样导入你的包。前任:

import your.package.name.R;

it fixes similar error for other projects of mine

它为我的其他项目修复了类似的错误

回答by George Nguyen

styleable is not supported http://developer.android.com/sdk/RELEASENOTES.html

不支持 styleable http://developer.android.com/sdk/RELEASENOTES.html

The android.R.styleable class and its fields were removed from the public API, to better ensure forward-compatibility for applications. The constants declared in android.R.styleable were platform-specific and subject to arbitrary change across versions, so were not suitable for use by applications. You can still access the platform's styleable attributes from your resources or code. To do so, declare a custom resource element using a <declare-styleable>in your project's res/values/R.attrsfile, then declare the attribute inside. For examples, see <sdk>/samples/ApiDemos/res/values/attrs.xml. For more information about custom resources, see Custom Layout Resources. Note that the android.R.styleable documentation is still provided in the SDK, but only as a reference of the platform's styleable attributes for the various elements.

android.R.styleable 类及其字段从公共 API 中删除,以更好地确保应用程序的向前兼容性。android.R.styleable 中声明的常量是特定于平台的,并且在不同版本之间会发生任意更改,因此不适合应用程序使用。您仍然可以从您的资源或代码访问平台的样式属性。为此,请<declare-styleable>在项目res/values/R.attrs文件中使用 a 声明自定义资源元素,然后在其中声明属性。有关示例,请参阅<sdk>/samples/ApiDemos/res/values/attrs.xml。有关自定义资源的更多信息,请参阅自定义布局资源。请注意,SDK 中仍提供 android.R.styleable 文档,但仅作为平台各种元素的可样式化属性的参考。

回答by Atul Dravid - White Pvt. Ltd.

I tried everything, but with no luck. The generated R.java was showing the stylable class but the compilation was showing "Stylable Not found". I just added the package name before R, after the change, everything is working fine now...

我尝试了一切,但没有运气。生成的 R.java 显示样式类,但编译显示“找不到样式”。我只是在 R 之前添加了包名,更改后,现在一切正常......

So, if your package name is com.example.test, then modify following code...

所以,如果你的包名是 com.example.test,那么修改下面的代码...

public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();

}

}

TO

public ImageAdapter(Context c) {
    mContext = c;
    TypedArray a = c.obtainStyledAttributes(com.example.test.R.styleable.Gallery1);
    mGalleryItemBackground = a.getResourceId(com.example.test.R.styleable.Gallery1_android_galleryItemBackground, 0);
    a.recycle();
}

回答by notenking

There are have a little error in the select answer,instead stylable with styleable

选择答案有一点错误,而是用 styleable 风格化

It's should like this:

应该是这样的:

<declare-styleable name="Gallery1"> 
    <attr name="android:galleryItemBackground" /> 
</declare-styleable>