Android中R.styleable 无法解析时候的解决办法


今天尝试编译Android SDK中APIDemos中的程序,调试到HelloGallery的时候,在下面这段代码中:

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

 

编译出错,提示说android.R.styleable unresolved,在网上查了下,说R.styleable在SDK1.5中已经不再支持,所以会出现这个错误。解决方法如下:

1.在res/values目录下新建attrs.xml,在其中添加如下内容:

 

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

 

2.修改HelloGallery.java,将出错的那段代码:

 

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

 

修改为:

 

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM