问题
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