阅读354 返回首页    go 京东网上商城


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

今天尝试编译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.重新运行就可以了

最后更新:2017-04-03 12:55:42

  上一篇:go python 多人聊天DOS版
  下一篇:go 函数中的形式参数和实际参数