我在下面添加了Activity,其中我尝试创建自己的自定义xml字符串,
从我想解析一个ColorStateList对象
public class MainActivity extends Activity {
final String colorVals = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">" +
"<item android:color=\"#333\" android:state_focused=\"true\" />" +
"<item android:color=\"#fff\" android:state_pressed=\"true\" />" +
"<item android:color=\"#666\" android:state_enabled=\"false\" />" +
"<item android:color=\"#222\"/>" +
"</selector>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
String xml = colorVals;
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
parser.setInput(is, "UTF-8");
Resources res = getResources();
ColorStateList colors = ColorStateList.createFromXml(res, parser);
TextView tv = new TextView(this);
tv.setTextColor(colors);
} catch (Exception e) {
Log.e("test", e.getMessage());
}
}
}
当我试图这样做时,我得到下一个错误:
10-22 16:23:04.006:E / test(16127):START_TAG(空)http://schemas.android.com/apk/res/android}android:color='#333'{http:// schemas.android.com/apk/res/android}android:state_focused='true'>@1:166在java.io.InputStreamReader@41dcc110:标签需要一个“机器人:颜色”属性 .
调试我创建的解析器,我在属性集中看到:
attr [i] = "Android",
attr [i 1] = "color",
我想我可能会错误地实例化解析器