Android中R.styleable 無法解析時候的解決辦法

今天嘗試編譯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();
        }

發佈了37 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章