使用AsyncTask时的运行时异常

时间:2015-08-12 17:48:35

标签: java android android-asynctask

我正在关注Head First Android开发,我被困在NASA日常图像适配器的第3章中,我通过stackOverFlow看了一下,你们建议我使用AsyncTask,[link]这是正在讨论这个问题的问题。 Android Head First "NASA daily image App"

这是我正在尝试的代码:

public class MainActivity extends ActionBarActivity {

IotdHandler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    IotdHandler handler=new IotdHandler();
    new AsyncTaskClass().execute();
}

public class IotdHandler extends DefaultHandler{
    private String url="http://www.nasa.gov/rss/image_of_the_day.rss";
    private boolean inUrl=false;
    private boolean inTitle=false;
    private boolean inDescription=false;
    private boolean inItem=false;
    private boolean inDate=false;
    private Bitmap image=null;
    private String title=null;
    private StringBuffer description=new StringBuffer();
    private String date= null;

    public void processFeed()
    {
        try{
            SAXParserFactory factory=SAXParserFactory.newInstance();
            SAXParser parser=factory.newSAXParser();
            XMLReader reader=parser.getXMLReader();
            reader.setContentHandler(this);
            InputStream inputStream=new URL(url).openStream();
            reader.parse(new InputSource(inputStream));

        }
        catch(Exception e)
        {

        }
    }

    private Bitmap getBitmap(String url)
    {
        try
        {
            HttpURLConnection connection=(HttpURLConnection) new URL(url).openConnection();
            connection.setDoInput(true);
            InputStream input=connection.getInputStream();
            Bitmap bitmap=BitmapFactory.decodeStream(input);
            input.close();
            return bitmap;
        }
        catch(IOException ioe)
        {
            return null;
        }
    }

    public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException
    {
        if(localName.equals("url"))
        {
            inUrl=true;
        }
        else
        {
            inUrl=false;
        }

        if(localName.startsWith("item"))
        {
            inItem=true;
        }
        else if(inItem)
        {
            if(localName.equals("title"))
            {
                inTitle=true;
            }
            else
            {
                inTitle=false;
            }

            if(localName.equals("description"))
            {
                inDescription=true;
            }
            else 
            {
                inDescription=false;
            }
            if(localName.equals("pubDate"))
            {
                inDate=true;
            }
            else
            {
                inDate=false;
            }
        }
    }

    public void characters (char ch[],int start,int length)
    {
        String chars=new String(ch).substring(start,start+length);
        if(inUrl && url==null){
            image=getBitmap(chars);
        }
        if(inTitle && title==null)
        {
            title=chars;
        }
        if(inDescription)
        {
            description.append(chars);
        }
        if(inDate && date==null)
        {
            date=chars;
        }
    }

    public Bitmap getImage() {return image;}
    public String getTitle() {return title;}
    public StringBuffer getDescription() {return description;}
    public String getDate() {return date;}
}

public class AsyncTaskClass extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void...params) {
        handler.processFeed();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
        super.onPostExecute(result);
    }
}

private void resetDisplay(String title,String date,Bitmap imageDisplay,StringBuffer description)
{
    TextView titleView=(TextView) findViewById(R.id.imageTitle);
    titleView.setText(title);

    TextView dateView=(TextView) findViewById(R.id.imageDate);
    dateView.setText(date);

    ImageView imageView=(ImageView) findViewById(R.id.imageDisplay);
    imageView.setImageBitmap(imageDisplay);

    TextView descriptionView = (TextView) findViewById(R.id.imageDescription);
    descriptionView.setText(description);
}

}

现在我说doInBackGround方法有问题(Void ... params)是什么意思?这是写它的正确方法吗? Eclipse给了我一个运行时异常,说:

08-12 20:42:29.894: E/AndroidRuntime(28819): FATAL EXCEPTION: AsyncTask #1
08-12 20:42:29.894: E/AndroidRuntime(28819): Process: com.example.rssimages, PID: 28819
08-12 20:42:29.894: E/AndroidRuntime(28819): java.lang.RuntimeException: An error occured while executing doInBackground()

谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

...被称为varargs。基本上你可以传递0-n(变量参数)。点击此处:https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

如果您关联某些代码,则可以更轻松地帮助您修复AsyncTask