doInBackground方法中的异步任务错误

时间:2015-06-18 14:31:32

标签: java android android-asynctask

我试过这段代码

 public class RSSFeed_SAXParser extends Activity {
String streamTitle = "";
ArrayList<String> mediaUrlList, imageUrlList, guidList, titleList,
        pubDateList, bitrateList, durationList;

/** Called when the activity is first created. */
public RSSFeed_SAXParser(String url) {
    mediaUrlList = new ArrayList<String>();
    imageUrlList = new ArrayList<String>();
    guidList = new ArrayList<String>();
    titleList = new ArrayList<String>();
    bitrateList = new ArrayList<String>();
    durationList = new ArrayList<String>();
    pubDateList = new ArrayList<String>();
    try {
        URL rssUrl = new URL(url);
        SAXParserFactory mySAXParserFactory = SAXParserFactory
                .newInstance();
        SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
        XMLReader myXMLReader = mySAXParser.getXMLReader();

        RSSHandler myRSSHandler = new RSSHandler();
        myXMLReader.setContentHandler(myRSSHandler);
        InputSource myInputSource = new InputSource(rssUrl.openStream());
        myXMLReader.parse(myInputSource);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private class RSSHandler extends DefaultHandler {

    private boolean item = false;
    private boolean title = false;
    private boolean guid = false;
    private boolean image = false;
    private boolean pubdate = false;
    private boolean media = false;
    int count = 0;
    long bitr = 0;
    String url, duration, bitrate;
    String imageUrlValue, guidValue, titleValue, pubDateValue;
    private boolean flagTitle = false,  flagImage = false, flagMedia = false;

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (localName.equals("item")) {
            item = true;
            count++;
        } else if (localName.equalsIgnoreCase("content") && item) {
            int w = 0;
            long bit = 0;
            try {
                w = Integer.parseInt(attributes.getValue("width"));
                bit = Long.parseLong(attributes.getValue("bitrate"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (1024 <= w && bit > 1100000) {
                bitrate = attributes.getValue("bitrate");
                url = attributes.getValue("url");
                duration = attributes.getValue("duration");
                Log.v("videoWidth" + localName,
                        "=" + attributes.getValue("width"));
                flagMedia = true;
            }else if( w == 640 && bit > bitr && !flagMedia){
                bitrate = attributes.getValue("bitrate");
                url = attributes.getValue("url");
                duration = attributes.getValue("duration");
                Log.v("640videoWidth" + localName,
                        "=" + attributes.getValue("width"));
            }
        } else if (localName.equalsIgnoreCase("thumbnail") && item
                && !flagImage) {
            image = true;
            flagImage = true;
            Log.v("ImageAttributes" + localName,
                    "=" + attributes.getValue("url"));
            imageUrlValue = attributes.getValue("url");
            //imageUrlList.add(attributes.getValue("url"));
        } else if (localName.equalsIgnoreCase("updated") && item) {
            pubdate = true;
        } else if (localName.equalsIgnoreCase("title") && item
                && !flagTitle) {
            title = true;
        } else if (localName.equalsIgnoreCase("clipid") && item) {
            guid = true;
        } else {
            // Log.v("=" + localName, "=" + qName);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (localName.equals("item")) {
            item = false;
            flagImage = false;
            flagTitle = false;
            // flagMedia = false;

            media = true;

            // flagMedia = true;
            Log.w("MediaAttributes" + guidValue, "===" + url);
            if(url != null)
            {
                mediaUrlList.add(url);
                bitrateList.add(bitrate);
                durationList.add((Integer.parseInt(duration)*1000)+"");
                guidList.add("Clip"+guidValue);
                titleList.add(titleValue);
                pubDateList.add(pubDateValue);
                imageUrlList.add(imageUrlValue);
            }
            flagMedia = false;
            bitr = 0;
            url = bitrate = duration = guidValue = titleValue = pubDateValue         = imageUrlValue = null;
            Log.v("=" + count, "=" + qName);
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (title && item) {
            title = false;
            flagTitle = true;
            Log.d("title=", "=" + new String(ch, start, length));
            //titleList.add(new String(ch, start, length));
            titleValue = new String(ch, start, length);
        } else if (guid && item) {
            guid = false;
            //guidList.add(new String(ch, start, length));
            Log.d("guid=", "=" + new String(ch, start, length));
            guidValue = new String(ch, start, length);
        } else if (image && item) {
            image = false;
            // Log.d("image=", "=" + new String(ch, start, length));
        } else if (pubdate && item) {
            pubdate = false;
            //pubDateList.add(new String(ch, start, length));
            pubDateValue = new String(ch, start, length);
            Log.d("pubdate=", "=" + new String(ch, start, length));
        } else if (media && item) {
            media = false;
            // Log.d("media=", "=" + new String(ch, start, length));
        }
    }
}

public ArrayList<String> getTitleList() {
    return titleList;
}

public ArrayList<String> getPubDateList() {
    return pubDateList;
}

public ArrayList<String> getImageUrllist() {
    return imageUrlList;
}

public ArrayList<String> getGuidlist() {
    return guidList;
}

public ArrayList<String> getDurationList() {
    return durationList;
}

public ArrayList<String> getBitrateUrllist() {
    return bitrateList;
}

public ArrayList<String> getVideoUrllist() {
    return mediaUrlList;
}

}

// --------------------------

06-18 07:14:31.662: E/AndroidRuntime(16819): FATAL EXCEPTION: AsyncTask #1
06-18 07:14:31.662: E/AndroidRuntime(16819): java.lang.RuntimeException: An error occured while executing doInBackground()
06-18 07:14:31.662: E/AndroidRuntime(16819):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.lang.Thread.run(Thread.java:856)
06-18 07:14:31.662: E/AndroidRuntime(16819): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-18 07:14:31.662: E/AndroidRuntime(16819):    at android.os.Handler.<init>(Handler.java:197)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at android.os.Handler.<init>(Handler.java:111)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at android.app.Activity.<init>(Activity.java:780)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at com.fusioni.xmlParser.RSSFeed_SAXParser.<init>(RSSFeed_SAXParser.java:27)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at com.fusioni.spacealabama.Splashscreen$MyTask.doInBackground(Splashscreen.java:111)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at com.fusioni.spacealabama.Splashscreen$MyTask.doInBackground(Splashscreen.java:1)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-18 07:14:31.662: E/AndroidRuntime(16819):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-18 07:14:31.662: E/AndroidRuntime(16819):    ... 3 more

请提前告诉我,我知道我做错了什么。

我得到的错误低于

$("#id").fadeIn(1000).delay(3000).fadeOut(1000);

1 个答案:

答案 0 :(得分:1)

两件大事错误:

  1. 您无法在new上致电Activity。它们是由系统根据Intent对象创建的。

  2. 您无法在Handler内创建new RSSHandler()个对象(通过AsyncTask调用)。它是一个在任意线程中运行的临时,短暂的后台处理器。 Handler要求Looper已启动并绑定到Thread的运行环境。