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();
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM