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.

4

2 回答 2

3

检查您的进口:

  • 错误的:Android.R
  • 正确的:com.example.yourproject.R

制作此自定义视图时,我遇到了同样的错误。也许当按照指导步骤进行操作时,辅助工具会自动插入这个错误的导入。

于 2014-12-27T07:29:03.317 回答
2

看来我导入了错误的 R 类。它应该是应用程序的 R 类而不是 android.R

于 2013-06-21T22:37:53.687 回答