how to instantiate an XmlPullParser in android-8?

霸气de小男生 提交于 2019-12-13 12:46:54

问题


I am trying to use an XMLPullParser to parse some XML for an android app. I am classpathing in android.jar from the android-8 SDK.

Alas, this code:

import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;

class InstantiateXMLPullParser {
  public static void main( String args[] ){
    XmlPullParser xpp = Xml.newPullParser();
  }
}

fails with this error:

Exception in thread "main" java.lang.RuntimeException: Stub!
        at android.util.Xml.newPullParser(Xml.java:15)
        at InstantiateXMLPullParser.main(InstantiateXMLPullParser.java:6)

I suspect I need to specify an implementation somewhere -- it looks like once upon a time Android used org.apache.harmony.xml.ExpatPullParser as its default pull parser, but that no longer seems to be the case.


回答1:


You seem to have more fundamental problems. java.lang.RuntimeException: Stub! will only come when you attempt to use android.jar outside of Android. Try building an Android application and running it on a device or emulator, and you will get your pull parser.




回答2:


From the API docs

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();


来源:https://stackoverflow.com/questions/3605043/how-to-instantiate-an-xmlpullparser-in-android-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!