AlertDialog不会显示在catch块中

时间:2011-05-13 09:47:58

标签: android try-catch alertdialog

我正在尝试从网上解析一些XML数据。大多数情况下,XML是干净的,并在我的应用程序中很好地显示。这非常有效。我还希望构建一些安全性,因此当存在错误的XML时,应用程序不会崩溃。

因此,当无法解析XML时,它会跳转到我的catch块,然后继续执行此操作:

try {
            Log.e("in try", "try");
            /* Create a URL we want to load some xml-data from. */
            URL url = new URL("http://172.21.150.140:80/scripts/cgiip.exe/WService=brAccentBe/Android/getVacatureDetails.html?Vacid=" + vacaid + "&Kantoorid=" + kantoorid);

            System.out.println("Url " + url);
            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/ 
            vacatureDetailsWebservice vs = new vacatureDetailsWebservice();
            xr.setContentHandler(vs);

            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            vaca = vs.getVacatures();


        }    
 catch (Exception e) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(JobDetails.this);
            builder.setTitle("Fout in XML");
            builder.setMessage("Er is een fout opgetreden in de data. Probeer het later nog eens");
            builder.setCancelable(false);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            }); 
            builder.create().show();
        }

但是对话框不会显示,该类继续运行并获得其他异常,因为XML未被解析。在onCreate中调用此方法。

有谁看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

请尝试下面的事情可能会起作用但不确定......

boolean bol = false;

尝试{

//你的代码

} catch(例外e){

bol = true;

}

     if (bol){

       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Fout in XML");
        builder.setMessage("Er is een fout opgetreden in de data. Probeer het later nog eens");
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        }); 
        builder.create().show();
    }