Android Hello, Gallery tutorial — “R.styleable cannot be resolved”

前端 未结 7 1937
一整个雨季
一整个雨季 2020-11-27 13:23

When working on the Hello, Gallery tutorial/sample app, after following the instructions on the site, Eclipse reported that R.styleable cannot be resolved.

What is t

7条回答
  •  孤街浪徒
    2020-11-27 14:16

    The reason for this problem is the resources they tell you to put into res/values/attrs.xml are:

    
    
        
            
        
    
    

    But then you get this adapter, which Eclipse can't figure out and frankly makes no sense:

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
        mGalleryItemBackground = a.getResourceId(
                android.R.styleable.Theme_galleryItemBackground, 0);
        a.recycle();
    }
    

    That's because you shouldn't have "android." preceeding the resources, the styleable name is Theme here but HelloGallery in the actual resource, and the galleryItemBackground puts android between the styleable name and the attribute like this: Theme_android_galleryItemBackground

    So if want the ImageAdapter method to work with the resources you're given, you should rewrite it like this:

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }
    

    For future problems regarding resources (R.* cannot be resolved type errors), examine /gen/R.java for what the resources are actually being named.

提交回复
热议问题