R.styleable.gallery报错,Android中R.styleable 无法解析时候的解决办法
2016-02-02 13:46
495 查看
今天尝试编译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();
}
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();
}
相关文章推荐
- Android Jni开发之创建Jni程序和打印log
- Android 中关于文件的读写操作
- Android 开发技巧集合
- Android开发学习之路--Activity之Intent
- Android开发学习之路--Activity之Intent
- Android使用DialogFragment创建对话框
- Android开发之ProgressDialog与ProgressBar
- 阅读《Android 从入门到精通》(21)——滚动视图
- Android动画
- Android Jni开发之基础概念介绍
- Android系统启动顺序
- 随笔:关于权限android.permission.SYSTEM_ALERT_WINDOW
- Android 事件总线OTTO用法快速入门
- java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
- 由于Android列表视图复用到之后的Imageview异步加载图片时候闪烁问题
- ULog远程日志——让Android调试更加方便直观
- Android学习-JAVA基础 (三)
- Android SDK 体系介绍
- Android代码资源的国际化
- Android事件机制全然解析