0

Currently creating an Android app. I have a class called Pollen, on top of my Fragment class, and I am dealing with exception handling within this second class.

Here is my Pollen class.

public static class Pollen
{

    @SuppressLint("SimpleDateFormat")
    public Pollen(int zipcode, Context context)
    {
                    this.context = context;
        this.zipcode = zipcode;
        Document doc;
        try
        {
            // pass address to 
            doc = Jsoup.connect("http://www.wunderground.com/DisplayPollen.asp?Zipcode=" + this.zipcode).get();

            // get "location" from XML
            Element location = doc.select("div.columns").first();
            this.location = location.text();

            // get "pollen type" from XML
            Element pollenType = doc.select("div.panel h3").first();
            this.pollenType = pollenType.text();

            SimpleDateFormat format = new SimpleDateFormat("EEE MMMM dd, yyyy");

            // add the four items of pollen and dates
            // to its respective list
            for(int i = 0; i < 4; i++)
            {
                Element dates = doc.select("td.text-center.even-four").get(i);
                Element levels = doc.select("td.levels").get(i);

                try
                {
                    pollenMap.put(format.parse(dates.text()), levels.text());
                }
                catch (ParseException e)
                {
                    Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
                    toast.show();
                    return;
                }
            }
        }
        catch (IOException e)
        {
            Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
            toast.show();
            return;
        }
    }
}

Since Pollen uses Network tasks, it's under an asynchronous thread which is irrelevant to this question.

Whenever Pollen is met under an exception, despite the Toast, the app would crash.

I was wondering - what is the accepted way to deal with exception on classes outside of the main class?

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169

1 Answers1

1

Your application is crashing because you are creating a toast from outsied the UI thread.

You cannot create a Toast from a class which is not running on the main thread(UI thread). But you can use the runOnUiThread (http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)) to execute code on the UI thread from your Pollen class.

YourActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    Toast toast = Toast.makeText(YourActivity.this, R.string.toast_parse_fail, Toast.LENGTH_LONG);
        toast.show();
                }
            });

For this to work you have to pass the Activity's context to you custom class and use the above method or you can throw exception and catch it from the activity and handle it there.

DanKodi
  • 3,550
  • 27
  • 26
  • I am getting this error: `Cannot make a static reference to the non-static method runOnUiThread(Runnable) from the type Activity` – theGreenCabbage Jun 01 '14 at 00:58
  • Why is your class Pollen, static? – DanKodi Jun 01 '14 at 01:03
  • Yes, it's static, because if it is not, I get this error: `No enclosing instance of type MainActivity is accessible. Must qualify the allocation with an enclosing instance of type MainActivity (e.g. x.new A() where x is an instance of MainActivity).` within my `Fragment` class (when I call `Pollen`). If you could suggest me an alternative, that would be terrific. – theGreenCabbage Jun 01 '14 at 01:04
  • So Pollen is an inner class of the MainActivity? – DanKodi Jun 01 '14 at 01:08
  • Yes, it's an inner class of `MainActivity`. It follows this code structure, from a previous question I asked: http://stackoverflow.com/questions/23973612/network-related-tasks-on-a-separate-thread – theGreenCabbage Jun 01 '14 at 01:10
  • try MainActivity.this.runOnUiThread(new Runnable() { public void run() { Toast toast = Toast.makeText(MainActivity.this, R.string.toast_parse_fail, Toast.LENGTH_LONG); toast.show(); } where MainActivity is you Activity }); – DanKodi Jun 01 '14 at 01:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54855/discussion-between-thegreencabbage-and-dankodi). – theGreenCabbage Jun 01 '14 at 01:18