5

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.

Community
  • 1
  • 1
Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79

2 Answers2

4

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Tin Luu
  • 1,557
  • 16
  • 22
2

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

Abdalrahman Shatou
  • 4,550
  • 6
  • 50
  • 79
  • If this is the answer, then please accept your own answer to help future visitors. That's how Stack Overflow works. See [How does accepting an answer work?](http://meta.stackexchange.com/q/5234/173448). – jww Dec 27 '14 at 07:36