Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlPullAttributes cannot be cast to XmlBlockParser

Custom xml file to view

This is an error produced from the above question

I managed to get the XML file saved into the assets folder, and loaded (and I can even navigate through the different parts of the parser and produce outputs from them) but when I try to pass the XmlPullParser to LayoutInflater I get the above error. I am not totally sure why it is happening though.

Below is the activity that I am using (the part of it that involves parsing the Xml file) as well as the xml file (called activity_main.sv) and the logcat.

Ignore the file extension of the xml file. I simply renamed it because the IDE was having fits about inclusion. If the xml file is saved in the res/layouts folder it works just fine (I know because I copied it directly out of the res/layouts folder).

public void onCreate(Bundle savedInstanceState){
...
     try {
          XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
          XmlPullParser parser = factory.newPullParser();
          InputStream in = getResources().getAssets().open("views/activity_main.sv");
          parser.setInput(new InputStreamReader(in));
          LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        The below line is line 73, the line which is giving me issue
          View view = inflate.inflate(parser, null);
          setContentView(view);
     }catch(Exception e){
          e.printStackTrace();
//        Consider the below line Log.d("TAG","Well fuck me it didnt work"). AppLog is a custom class I created to help manage logging.
          AppLog.log("Well fuck me it didnt work");
     }
}

And my basic relativelayout saved as "activity_main.sv"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

And finally the logcat output

10-24 19:36:55.273: W/EGL_emulation(4724): eglSurfaceAttrib not implemented
10-24 19:36:55.273: W/OpenGLRenderer(4724): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa34192a0, error=EGL_SUCCESS
10-24 19:47:15.252: W/System.err(8074): java.lang.ClassCastException: android.util.XmlPullAttributes cannot be cast to 

android.content.res.XmlBlock$Parser
10-24 19:47:15.253: W/System.err(8074):     at android.content.res.Resources$Theme.obtainStyledAttributes

(Resources.java:1483)
10-24 19:47:15.253: W/System.err(8074):     at android.content.Context.obtainStyledAttributes(Context.java:460)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
10-24 19:47:15.253: W/System.err(8074):     at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
10-24 19:47:15.253: W/System.err(8074):     at pro.bladebeat.intentplayground.MainActivity.onCreate

(MainActivity.java:73)
10-24 19:47:15.253: W/System.err(8074):     at android.app.Activity.performCreate(Activity.java:5933)
10-24 19:47:15.253: W/System.err(8074):     at android.app.Instrumentation.callActivityOnCreate

(Instrumentation.java:1105)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.performLaunchActivity

(ActivityThread.java:2251)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.handleLaunchActivity

(ActivityThread.java:2360)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
10-24 19:47:15.253: W/System.err(8074):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 19:47:15.253: W/System.err(8074):     at android.os.Looper.loop(Looper.java:135)
10-24 19:47:15.253: W/System.err(8074):     at android.app.ActivityThread.main(ActivityThread.java:5221)
10-24 19:47:15.253: W/System.err(8074):     at java.lang.reflect.Method.invoke(Native Method)
10-24 19:47:15.253: W/System.err(8074):     at java.lang.reflect.Method.invoke(Method.java:372)
10-24 19:47:15.253: W/System.err(8074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

(ZygoteInit.java:899)
10-24 19:47:15.253: W/System.err(8074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-24 19:47:15.257: D/AppLog(8074): Well fuck me it didnt work

EDIT After adding the factory.setValidating(true) statement. I have a different error now.

11-04 14:59:52.682: W/System.err(4664): org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#validation (position:START_DOCUMENT null@1:1) 
11-04 14:59:52.684: W/System.err(4664):     at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2100)
11-04 14:59:52.684: W/System.err(4664):     at org.xmlpull.v1.XmlPullParserFactory.newPullParser(XmlPullParserFactory.java:135)
11-04 14:59:52.684: W/System.err(4664):     at pro.bladebeat.intentplayground.MainActivity.onCreate(MainActivity.java:36)
11-04 14:59:52.684: W/System.err(4664):     at android.app.Activity.performCreate(Activity.java:5933)
11-04 14:59:52.684: W/System.err(4664):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-04 14:59:52.684: W/System.err(4664):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 14:59:52.684: W/System.err(4664):     at android.os.Looper.loop(Looper.java:135)
11-04 14:59:52.684: W/System.err(4664):     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-04 14:59:52.684: W/System.err(4664):     at java.lang.reflect.Method.invoke(Native Method)
11-04 14:59:52.684: W/System.err(4664):     at java.lang.reflect.Method.invoke(Method.java:372)
11-04 14:59:52.684: W/System.err(4664):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-04 14:59:52.684: W/System.err(4664):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
like image 620
miversen33 Avatar asked Oct 25 '15 16:10

miversen33


People also ask

What is XML Pull Parser in Android?

In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser.

How XML data is parsed?

The parser reads an XML document from the beginning to the end. When it encounters a node in the document, it generates an event that triggers the corresponding event handler for that node. The handler thus applies the application logic to process the node specifically.

What is pull parser?

XML Pull Parser is an interface that defines parsing functionality provided in XMLPULL V1 API (visit this website to learn more about API and its implementations).


2 Answers

Doc LayoutInflate

look this:

Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

like image 94
Santi Avatar answered Nov 03 '22 00:11

Santi


I know it is an old question but I want to give an answer for those who want to use this method or faced with the problem recently.

I studied android source code about five hours. XmlBlock is a private class and XmlBlock.Parser is inside it so can be accessed only by classes in android.content.res. If you use this method with a xml got from method Resources.getXml(id) it will work without any problem(!) because getXml calls a function that creates a XmlBlock.Parser with its special algorithm designed for application internal resources. So DON'T WASTE your time to get it worked (XmlResourceParser or anything like that cannot be cast to XmlBlock.Parser)

like image 44
momt99 Avatar answered Nov 03 '22 01:11

momt99