I have a code that creates a kml file using XMLserializer
. It works fine creating the different polygons, but I'm having an error with closing the xml that I really don't understand.
02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err: java.lang.ArrayIndexOutOfBoundsException: length=24; index=-3
02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err: at org.kxml2.io.KXmlSerializer.endTag(KXmlSerializer.java:493)
02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err: at com.example.accgps.KMLHelper.SerialEnd(KMLHelper.java:150)
02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err: at com.example.accgps.RefreshKML$1.run(RefreshKML.java:283)
02-09 10:03:02.919 24712-26301/com.example.accgps W/System.err: at java.lang.Thread.run(Thread.java:818)*
The call to the SerialEnd()
is done after creating the polygons, which work fine, so I don't know why it is out of bounds:
for (s = 0; s < PolyArray.size(); s++) {
polygon = PolyArray.get(s);
kH.SerialPlacePol("Tramo " + i, polygon);
i++;
}
Log.i(TAG, "RefreshKML: se han creado " + (i - 1) + " tramos");
editor.putInt("poligonos", i);
editor.apply();
} else {
Toast.makeText(getApplicationContext(),"No existen datos suficientes para "+
"generar el archivo KML",Toast.LENGTH_SHORT).show();
}
} else {
Log.d(TAG, "RefreshKML: No hay datos de GPS suficientes");
}
if (end) { //Solo cierra el archivo si se termina la recopilación de datos
kH.SerialEnd();
end = false; //En caso de que no se completó esta opción
copyAllFiles();
}
The error is provoked in the first line of the SerialEnd()
method:
public void SerialEnd () {
try{
serializer.endTag(null,"Document"); //THIS IS THE LINE WITH THE ERROR
serializer.endTag(null,"kml");
//serializer.endDocument();
//serializer.flush();
fileos.close();
Log.d(TAG,"KMLHelper: SerialEnd cierra archivo KML");
}catch(Exception e){
Log.d(TAG,"KMLHelper: Exception occured in SerialEnd");
e.printStackTrace();
}
}
I have tried to find an answer in other questions, but I didn't find the proper solution, maybe the serializer is closed, but I haven't done that yet
This is how I create the XMLserializer
, in case you see a problem in here:
public KMLHelper(File file) {
try{
serializer.endTag(null, "Document");
serializer.endTag(null, "kml");
serializer.endDocument();
serializer.flush();
Log.d(TAG,"KMLHelper: SerialEnd cierra archivo KML");
}catch(Exception e){
Log.d(TAG,"KMLHelper: Exception occurred in SerialEnd");
e.printStackTrace();
}finally {
try {fileos.close();} catch (IOException io) {io.printStackTrace();}
}
}
Edit: I have made some changes in the SerialEnd()
, put the closure of the file in the finally and uncommented the endDocument method. I've also controlled the access to the thread that creates the xml file with a synchronized to the creation of the intent that is controlling the creation of the XML file with all the polygons.
Solved! The problem was that it was creating 2 XMLserializer working on the same file. I changed how the service calls the method, now I'm using a background Thread to make the calls and I control the execution with a join(), so there is only 1 execution at a time.