Android示例HelloGallery中R.styleable unresolved的解决办法

来源:互联网 发布:海岛火炮升级数据 编辑:程序博客网 时间:2024/05/21 16:58

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

[java] view plaincopy
  1. public ImageAdapter(Context c) {  
  2.         mContext = c;  
  3.         TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);  
  4.         mGalleryItemBackground = a.getResourceId(  
  5.                 android.R.styleable.Theme_galleryItemBackground, 0);  
  6.         a.recycle();  
  7.     }  
 

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

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

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="Gallery">  
  4.         <attr name="android:galleryItemBackground">  
  5.         </attr>  
  6.     </declare-styleable>  
  7. </resources>  

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

[java] view plaincopy
  1. public ImageAdapter(Context c) {  
  2.         mContext = c;  
  3.         TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);  
  4.         mGalleryItemBackground = a.getResourceId(  
  5.                 android.R.styleable.Theme_galleryItemBackground, 0);  
  6.         a.recycle();  
  7.     }  

修改为:

[java] view plaincopy
  1. public ImageAdapter(Context c) {  
  2.             mContext = c;  
  3.             TypedArray a = obtainStyledAttributes(R.styleable.Gallery);  
  4.             mGalleryItemBackground = a.getResourceId(  
  5.                     R.styleable.Gallery_android_galleryItemBackground, 0);  
  6.             a.recycle();  
  7.         }  

3.重新运行就可以了

0 0
原创粉丝点击