开发者

AsyncTask Class parses XML

开发者 https://www.devze.com 2023-04-04 07:07 出处:网络
This code worked when I had it in the UI thread but wanted to move it into a AsyncTask class.I have just finished reading up how to do this but when I run the app it is now crashing when it starts rea

This code worked when I had it in the UI thread but wanted to move it into a AsyncTask class. I have just finished reading up how to do this but when I run the app it is now crashing when it starts reading the class.

The class calls and parses an XML files before loading it into a List. This is my first Android app, so any help you can provide is appricated.

Thanks

public class DownloadXml extends AsyncTask<String, Void, String>
{       
    private ProgressDialog pd;
    protected String doInBackground(String... platform) 
    {
        pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

        try
        {
            BaseFeedParser parser = new BaseFeedParser();
            messages = parser.parse();
        }
        catch (Throwable t)
        {
            Log.e("RetailApp",t.getMessage(),t);
        }
        return rowPlatform;
    }


    protected void onPostExecute(String rowPlatform)
    {
        productList = new ArrayList<HashMap<String, Object>>();

            for (Message msg : messages)
            {   
                if(msg.getPlatform().compareTo(rowPlatform) == 0 )
                {   
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("Title", msg.getTitle());
                    map.put("Brand", msg.getBrand());
                    map.put("Price", "$" + msg.getPrice());

                    try
                    {
                        String picture[] = msg.getImage().split("-large.jpg");
                        URL pictureURL = new URL(picture[0] + "-medium.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(pictureURL.openStream());
                        map.put("Img",bitmap);
                    }
                catch (Throwable t)
                {
                    Log.e("RetailApp",t.getMessage(),t);
                }
                productList.add(map);
                } 
            }

Edit: I have moved the 'for' loop开发者_C百科 into onPostExecute and that seems to improve the code but I am still receiving a 'unregistered handle still locked' error. Any ideas?

Thanks for the idea Rob but no luck, have removed loop and Toast from background... new code above. Am receiving the error "Uncaught handler: thread AsycTask #1 exiting due to uncaught exception"


Use your LogCat. The AndroidRuntime log will give you a full stack trace and a strongly typed exception that will show you exactly why it crashed.

I'm pretty sure it's the fact that you're trying to show a ProgressDialog from the background thread. You should do that on the UI thread just before you execute your AsyncTask. You can dismiss it in onPostExecute, since that runs back on the UI thread


this

pd = ProgressDialog.show(CategoryData.this, "Please Wait...", "Loading games");

goes in onPreExecute(), which is called on the UI Thread before execute is started.

0

精彩评论

暂无评论...
验证码 换一张
取 消