I have created an XML file called “temp.gpx” in internal storage, and now I want to parse it. I wirte this method to do the parsing and obtaing GPS coordinates and some other stuff. DOCUMENT_START is detected (its Log.d line is written), but then I’m getting an exception, without any clue of which exact line is causing it.
The exception is “Unable to add window: token null is not for an application”. Log.d(TAG, “next”) is never written.
private void procesarGPX() throws XmlPullParserException, IOException {
String tag = new String();
float lat, lon;
trackData = new TrackData(true, true);
FileInputStream leerFichero = getApplicationContext().openFileInput("temp.gpx");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(leerFichero, null);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_DOCUMENT){
Log.d(TAG, "START_DOC");
}
else if(eventType == XmlPullParser.START_TAG){
Log.d(TAG, "START_TAG");
tag = xpp.getName();
if(tag.equals("name")) boolName = true;
else if(tag.equals("trkpt")){
lat = Float.parseFloat(xpp.getAttributeValue(null, "lat"));
lon = Float.parseFloat(xpp.getAttributeValue(null, "lon"));
if(lat*(-1)<=180 && lon*(-1)<=180) trackData.addPoint(new GeoPoint((int)(lat*1E6), (int)(lon*1E6)));
}
else if(tag.equals("ele")) boolEle = true;
else if(tag.equals("time")) boolTime = true;
else if(tag.equals("gpx")){
Log.d(TAG, "START_TAG es del tipo gpx");
trackData.setAutor(xpp.getAttributeValue(null, "creator"));
trackData.setVersion(xpp.getAttributeValue(null, "version"));
}
}
else if(eventType == XmlPullParser.END_TAG){
Log.d(TAG, "END_TAG");
if(tag.equals("name")) boolName = false;
else if(tag.equals("ele")) boolEle = false;
else if(tag.equals("time")) boolTime = false;
}
else if(eventType == XmlPullParser.TEXT){
Log.d(TAG, "TEXT");
if(boolName) trackData.setName(xpp.getText());
else if(boolEle) trackData.addElevationValue(Float.parseFloat(xpp.getText()));
else if(boolTime) parseTime(xpp.getText());
}
eventType = xpp.next();Log.d(TAG, "next()");
}
Log.d(TAG, "GPX processed");
Intent mapaIntent = new Intent(this, pfc.uniovi.MapaActivity.class);
startActivity(mapaIntent);
}
Code is right. After a lot of debug, I saw it crashed when the first line was still not an XML line. As long as you care to pass just the XML lines, the code will work. I’ll accept the answer so anyone who need the code gets it,