下面是我使用的代码:
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();
}
}下面是attrs.xml文件(添加到values文件夹):
<?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>这些问题的答案不能解决这个问题。请不要认为我的问题是重复的。
更新:似乎我导入了错误的R类。它应该是应用程序的R类,而不是android.R类。
发布于 2014-12-27 07:29:03
检查您的进口:
Android.Rcom.example.yourproject.R在创建这个自定义视图时,我也犯了同样的错误。也许当遵循指导步骤时,助手工具会自动插入这个错误的导入。
发布于 2013-06-21 22:37:53
好像我引进了错误的R级。它应该是应用程序的R级,而不是android.R。
https://stackoverflow.com/questions/17242720
复制相似问题