this code parses a locally stored xml file and then creates a dynamic UI from the parsed data, though i haven’t completed the UI part. It was working fine till yesterday and now suddenly it is not responding.
public class XML_PARSER extends Activity {
String TAG= "XML_PARSER";
List optionList = new ArrayList(); ;
Document dom;
Document doc;
Menu mymenu=null;
String msg=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
new ParseXML().execute();
}
private class ParseXML extends AsyncTask<Integer, Integer, Document>{
@Override
protected Document doInBackground(Integer... params) {
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
Document dom1=null;
try {
//Uri uri = Uri.parse("android.resource://com.example.xml_parser/raw/options");
InputStream is=getResources().openRawResource(R.raw.options);
DocumentBuilder db = dbf.newDocumentBuilder();
dom1=db.parse(is);
Log.i(TAG,"parsing done");
}
catch(ParserConfigurationException pce){
pce.printStackTrace();
}
catch(SAXException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
return dom1;
}
@Override
public void onPostExecute(Document d) {
ParseDocument(d);
//onCreateOptionsMenu(mymenu);
text();
}
}
@SuppressWarnings("unchecked")
public void ParseDocument(Document dom){
Element docEle = dom.getDocumentElement();
Node node;
NodeList n1= docEle.getElementsByTagName("Option");
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
node=n1.item(i);
Element e1=(Element)n1.item(i);
it is somehow getting stuck at this getOption() as the log is not getting printed if written below while it is getting printed if written above it.
Option e = getOption(e1,node);
Log.i(TAG,"Parse Document reached");
optionList.add(e);
}
}
}
private Option getOption(Element el,Node parent){
String name= getTextValue(el,"Name");
String type = el.getAttribute("type");
Option op = new Option(name,type);
fillSubOptions(op, el, parent);
return op;
}
private String getTextValue(Element ele, String tagName){
String textVal=null;
NodeList n1 = ele.getElementsByTagName(tagName);
if(n1!=null && n1.getLength()>0){
Element el= (Element)n1.item(0);
textVal =el.getFirstChild().getNodeValue();
}
return textVal;
}
private void fillSubOptions(Option op, Element el, Node parent){
Element ele;
Node node;
String name = null;
NodeList n1 = el.getElementsByTagName("Option");
int count =0;
if(n1!=null && n1.getLength()>0){
for(int i=0;i<n1.getLength();i++){
ele = (Element)n1.item(i);
node= n1.item(i);
if((node.getParentNode()).equals(parent)){
name= getTextValue(ele, "Name");
count= op.printSubOptions(count);
op.setSubOptions(name);
}
}
}
}
@SuppressWarnings("unchecked")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mymenu=menu;
Iterator<Option> it= optionList.iterator();
int count=0;
while(it.hasNext()){
Option e= (Option)it.next();
if(e.getType().equals("menu")){
count++;
menu.add(0,count,0,e.getName());
}
}
getMenuInflater().inflate(R.menu.parser, menu);
return true;
}
public class Option {
String name;
String type;
String parent;
int count;
ArrayList<String> subOptions;
Option(String name,String type)
{
setName(name);
setType(type);
subOptions = new ArrayList<String>();
parent = null;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setSubOptions(String subOption) {
(this.subOptions).add(subOption);
}
public String getName() {
return name;
}
public int printSubOptions(int count) {
Iterator<String> it = (this.subOptions).iterator();
if(it.hasNext()){
while(it.hasNext()){
count++;
}
}
return count;
}
public String getType() {
return type;
}
}
As the methods called in the
onPostExecutedgets the control of the UI thread,I hope that may be the cause as the parsing that you are doing is still done on the UI thread and so your application stops responding as it would beparsingthe data.Use onPreExecute() to start the
ProgressDialoglike this way.Declare theProgressDialogGloballyat the Top.Start it in the
onPreExecuteMethod.Call these both methods in doInBackground Itself at the Bottom.Don’t call it in the PostExecute Method so that it gets
executedin the BackGroundNow, in the PostExecute method,dismiss the progressDialog.