Sam Ibraheem
Sam Ibraheem

Reputation: 157

run-time exception when using AsyncTask

I'm following the Head First Android Development, I'm stuck in Chapter 3 at the NASA daily image adapter, I've looked through stackOverFlow and on of you guys suggested that I use the AsyncTask,[link] this is the question that was discussing the issue. Android Head First "NASA daily image App"

and here's the code that I'm trying with:

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

}

Now I had a problem with the doInBackGround method it says (Void...params) what does that mean? Is it the right way to write it? Eclipse is giving me a run-time exception that says:

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

thank you for your help

Upvotes: 3

Views: 58

Answers (1)

Charles Durham
Charles Durham

Reputation: 2495

The ... is something known as varargs. Essentially you can pass 0-n (variable arguments). See more here: https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

If you link some code it will be easier to help you fix your AsyncTask.

Upvotes: 2

Related Questions