Android你好,画廊教程 - “R.styleable无法解决” [英] Android Hello, Gallery tutorial -- "R.styleable cannot be resolved"

查看:181
本文介绍了Android你好,画廊教程 - “R.styleable无法解决”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

追踪该网站上的说明,Eclipse报告说,R.styleable无法解决。



这个错误的原因是什么,如何修复或解决方案

每个这个线程,R.styleable已经从android 1.5和更高版本中删除。



有一个数字获取样品的方法最简单,我发现最简单的是Justin Anderson在上面链接的线程中推荐:


  1. 创建一个名为resources.xml的新XML文件,其中包含以下内容:

     <?xml version =1.0编码= UTF-8 >?; 
    < resources>
    < declare-styleable name =Gallery1>
    < attr name =android:galleryItemBackground/>
    < / declare-styleable>
    < / resources>


  2. 将XML文件放在res\values目录中(与strings.xml一起) / p>


  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();
    }


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


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

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

解决方案

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

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:

  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();
    }
    

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.

这篇关于Android你好,画廊教程 - “R.styleable无法解决”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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