Android示例HelloGallery中R.styleable unresolved的解決辦法
今天嚐試編譯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();
- }
3.重新運行就可以了
最後更新:2017-04-03 12:55:42