我有一个字符串输入,其中包含一个带字符串Fromat的Layout.xml。
// String that contains the Layout.xml :
String concat ;
// Create the XmlPullParser from the String format
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader (concat) );
// create le The LayoutInflater
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(xpp, null);
我有这个错误:
03-12 08:23:12.876:W / System.err(937):android.view.InflateException: START_TAG http://schemas.android.com/apk/res/android}android:orientation='vertical' {http://schemas.android.com/apk/res/android}机器人:layout_width = 'FILL_PARENT' {http://schemas.android.com/apk/res/android}机器人:layout_height = 'FILL_PARENT' > @ 1:226 在java.io.StringReader@44f50508中:错误膨胀类
请帮忙吗?
答案 0 :(得分:3)
似乎Inflater只接受XmlBlock。
我已经编写了一个方法来执行此操作,您可以参考项目站点: https://github.com/liudongmiao/preference-fragment-compat/blob/master/src/me/piebridge/android/preference/PreferenceFragment.java#L202
主要代码如下:
// byte[] data = ...
// bytes of compiled xml (unzip the apk, get the bytes from res/layout*/*.xml)
// XmlBlock block = new XmlBlock(data);
Class<?> clazz = Class.forName("android.content.res.XmlBlock");
Constructor<?> constructor = clazz.getDeclaredConstructor(byte[].class);
constructor.setAccessible(true);
Object block = constructor.newInstance(data);
// XmlPullParser parser = block.newParser();
Method method = clazz.getDeclaredMethod("newParser");
method.setAccessible(true);
XmlPullParser parser = method.invoke(block);