应用程序说没有响应,即使我使用AsyncTask来减轻主线程上的负载

时间:2012-07-24 04:53:24

标签: java android xml xml-parsing dynamic-ui

此代码解析本地存储的xml文件,然后从解析的数据创建动态UI,但我还没有完成UI部分。它一直工作到昨天,现在突然它没有响应。

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);

它会以某种方式卡在这个getOption()上,因为如果在上面写下来的话,如果在下面写下来的话就不会打印日志。

            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;

}


}

1 个答案:

答案 0 :(得分:2)

由于onPostExecuted中调用的方法可以控制 UI线程,我希望这可能是你正在做的解析的原因仍然在 UI线程上完成,因此您的应用程序停止响应,因为它将是parsing数据。

使用 onPreExecute()以这种方式启动ProgressDialog。在顶部声明ProgressDialog Globally

ProgressDialog pd;

onPreExecute方法中启动

 @Override
    public void onPreExecute() {
          pd=ProgressDialog.show(myActivity.this,"","Please Wait");
        }

在底部调用 doInBackground 本身的这两种方法。不要在 PostExecute 方法中调用它,以便它在executed > BackGround

ParseDocument(d); 
text();
在PostExecute方法中

现在,关闭progressDialog。

@Override
    public void onPostExecute(Document d) {
          pd.dismiss();  //Dismiss the Progress Dialog 
        }