閱讀354 返回首頁    go 阿裏雲 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 函數中的形式參數和實際參數