How do I create a LayoutInflater for the XmlPullParser as input?
I have a String Input containing a Layout.xml with a String Fromat.
// 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);
I have this error:
03-12 08: 23: 12.876: W / System.err (937): android.view.InflateException: START_TAG http://schemas.android.com/apk/res/androidfandroid:orientation='vertical '{ http : //schemas.android.com/apk/res/android } android: layout_width = 'fill_parent' { http://schemas.android.com/apk/res/android } android: layout_height = 'fill_parent'> @ 1: 226 at java.io.StringReader@44f50508 : class bloat error
Help me please?
+3
source to share
1 answer
The Inflater seems to only accept XmlBlock.
I wrote a method for this, you can link to the project site: https://github.com/liudongmiao/preference-fragment-compat/blob/master/src/me/piebridge/android/preference/PreferenceFragment.java#L202
Basic codes:
// 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);
+3
source to share