取消Toast上的操作

时间:2014-06-01 23:54:08

标签: java android multithreading

我为我的Android项目编写了一个JSoup HTML抓取类。此自定义类将用户输入的邮政编码放入构造函数中,并解析该HTML。它在我的主线程的异步线程中工作。由于没有正确的方法来处理不正确的邮政编码,我在特定元素中检查了null,具体来说:

        if(doc.select("div.columns").first().text() == null)
        {
            ((Activity) context).runOnUiThread(new Runnable() {
                public void run() 
                {
                    Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
                    toast.show();
                    return;
                }
            });
        }

如果该特定元素为null(意味着此邮政编码不存在此类数据),则会为用户创建一个祝酒词。至于为什么try在这种情况下无法工作,这是因为JSoup和Java不知道解析是否失败,因为网页仍然可以正常加载;只有,没有数据,这会使应用程序从NullPointerException中跟随它后面的代码崩溃。

尽管我使用Toast进行了.runOnUiThread异常处理,但我的应用仍会崩溃。是否有任何特定方法可以取消遵循null检查方法的操作?我知道我崩溃了,因为Toast没有取消我的操作,并且正在执行以下代码,导致我的NullPointerExceptions

发布是我的完整Pollen构造函数。

        public Pollen(int zipcode, final Context context)
        {
            this.context = context;
            this.zipcode = zipcode;
            Document doc;

            try
            {
                // pass address to Wunderground's website using our inputted zipcode
                doc = Jsoup.connect("http://www.wunderground.com/DisplayPollen.asp?Zipcode=" + this.zipcode).get();

                if(doc.select("div.columns").first().text() == null)
                {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        public void run() 
                        {
                            Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
                            toast.show();
                            return;
                        }
                    });
                }

                // 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)
                    {
                        ((Activity) context).runOnUiThread(new Runnable() {
                            public void run() 
                            {
                                Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
                                toast.show();
                                return;
                            }
                        });
                    }
                }
            }
            catch (IOException e)
            {
                ((Activity) context).runOnUiThread(new Runnable() {
                    public void run() 
                    {
                        Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
                        toast.show();
                        return;
                    }
                });
            }
        }

tl; dr 我仍然希望显示Toast - 但我想要on-Toast,取消所有跟随它的操作,因为我知道如果此特定Toast显示,如果其后面的代码执行,则会导致应用程序崩溃。

1 个答案:

答案 0 :(得分:0)

既然你已经在try-catch - 阻止,你不能抛出异常吗?

public Pollen(int zipcode, final Context context) {
    this.context = context;
    this.zipcode = zipcode;
    Document doc;

    try {
       // pass address to Wunderground's website using our inputted zipcode
       doc = Jsoup.connect("http://www.wunderground.com/DisplayPollen.asp?Zipcode=" + this.zipcode).get();

       if(doc.select("div.columns").first().text() == null) {
           // Oh no! div.colums is empty. Lets throw an exception, which
           // will prevent the code below from executing.
           throw new IllegalStateException("div.columns is NULL");
       }

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

           // Removed nested try-catch block
           pollenMap.put(format.parse(dates.text()), levels.text());
       }
    } catch (IOException e) {
        displayToast(context);
    } catch (ParseException e) {
        // We catch the ParseException here instead of nesting try-catch blocks.
        displayToast(context);
    } catch (IllegalStateException e) {
        // Catch the IllegalStateException thrown when div.columns was null,
        // and let the user know what went wrong.
        displayToast(context);
    }
}

private void displayToast(Context context) {
    ((Activity) context).runOnUiThread(new Runnable() {
        public void run() {
            Toast toast = Toast.makeText(context, R.string.toast_parse_fail, Toast.LENGTH_LONG);
            toast.show();
        }
    });
}