简体   繁体   中英

Whenever I am running my android app, it says “XYZ is not responding”

It was running perfectly fine till yesterday... I am new to android programming , Any help would be great.. In my app I am parsing a downloaded xml file and then creating a dynamic UI... Here is a snippet of my code..

private class ParseXML extends AsyncTask<Integer, Integer, Document>{

    public void onPostExecute(Document d) {


        ParseDocument(d); }



    @Override
protected Document doInBackground(Integer... params) {
    // TODO Auto-generated method stub
     DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();

    try {
        //Uri uri = Uri.parse("android.resource://com.example.xml_parser/raw/options");
        InputStream is=getResources().openRawResource(R.raw.options);

        DocumentBuilder db = dbf.newDocumentBuilder();
        dom=db.parse(is);
        Log.i(TAG,"parsing done");

    }

    catch(ParserConfigurationException pce){
        pce.printStackTrace();
    }
    catch(SAXException se){
        se.printStackTrace();
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
    return dom;


}

The mistake is , you are trying to parse a XML in your Main UI thread. This means that you are restricting the user from performing actions with the UI. And in Andorid it is to be noted that, whenever a application doesn't respond for more the 5 seconds you will be throwed with Applicaation Not Responding Dialog.

So what you have to do is, use AsyncTask to parse your XML and then update your Ui from it.

Here is a example ,

https://stackoverflow.com/a/5806402/603744

AsyncTask contains three methods namely,

  1. doInBackground -which is equivalent to your Thread.
  2. onPostExcecute-Called once doInBackground completes its Task.
  3. onPreExecute-Called before doInBackground gets Called.

The 2nd and 3rd methods are related to UI, and you can perform any UI based actions here, and 1st method is your Background thread where you have to do your Immense Calculations, thus not affecting your UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM