2012-03-12 33 views
3

我有一個字符串輸入,其中包含一個帶String Fromat的Layout.xml。如何創建一個LayoutInflater給定XmlPullParser作爲輸入?

// 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); 

我有這個錯誤:

八月3日至12日:23:12.876:W/System.err的(937):android.view.InflateException: START_TAG HTTP://在java中的android:layout_height ='fill_parent'> @ 1:android:layout_width ='fill_parent' {http://schemas.android.com/apk/res/android} [email protected]:錯誤膨脹類

請幫忙嗎?

回答

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); 
相關問題