Styleable cannot be resolved

4.5k Views Asked by At

Here is the code I am using:

public ASSwitch(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray sharedTypedArray = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.ASSwitch,
            0, 0);

       try {
           onText = sharedTypedArray.getText(R.styleable.ASSwtich_onText, null);

       } finally {
           sharedTypedArray.recycle();
       }
}

Here is the attrs.xml file (added to values folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ASSwitch">
        <attr name="onText" format="string" />
        <attr name="offText" format="string" />
        <attr name="onState" format="boolean" />
        <attr name="toogleDrawable" format="string" />
        <attr name="frameDrawable" format="string" />
    </declare-styleable>
</resources>

The answers in these questions couldn't fix the problem. Please don't consider my question as duplicate.


Update: It seems that I was importing the wrong R class. It shall be the application's R class not android.R.

2

There are 2 best solutions below

0
On

Check your imports:

  • Wrong: Android.R
  • Correct: com.example.yourproject.R

I had the same error when made this customized view. Maybe when follow the guiding step, the helper tool automatically inserts this wrong import.

1
On

It seems that I was importing the wrong R class. It shall be the application's R class not android.R