我想在XML无法解析时捕获异常。因为我对正在创建的XML没有任何影响,所以当出现问题时(例如错误的标签,奇怪的标志等),我不会捕获异常。

我创建了以下代码,但是会导致崩溃。我在代码下添加了LogCat。我不太了解LogCat告诉我什么...有人提示我吗?谢谢

private void getVacatures(){
    functie = getIntent().getIntExtra("Functie", 0);
    stat =getIntent().getIntExtra("Statuut", 0);
    regio = getIntent().getIntExtra("Straal", 0);
    opl = getIntent().getIntExtra("Opleiding", 0);
    final AlertDialog.Builder builder = new AlertDialog.Builder(JobList.this);

        try {
            Log.e("in try", "try");
            /* Create a URL we want to load some xml-data from. */

            URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");
            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*/
            vacatureWebservice vs = new vacatureWebservice();
            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. */
            arrVacatures = vs.getVacatures();
        } catch (Exception e) {
            builder.setTitle(R.string.Fouttitel);
            builder.setMessage(R.string.XMLfout);
            builder.setCancelable(false);
            builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {

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

在onCreate()中,我这样做:
Handler handler = new Handler(); // This code is in the main UI activity class

    handler.post(runnable= new Runnable(){
        public void run(){

            getVacatures();
            runOnUiThread(returnRes);
        }
    });
    Thread thread = new Thread(null, runnable, "MagentoBackground");
    thread.start();

LogCat错误:http://pastebin.com/X5uk9cex

最佳答案

您正在尝试从名为MagentoBackground的工作线程更新Ui。您需要在主线程中完成您的Ui工作。尝试使用Handler

这是Handler的一些教程

  • Tutorial 1
  • Tutorial 2

  • 再次在您使用的URL像
      URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");
    

    URL将永远不会调用。您需要做的是将xml保留在Web服务器中,然后对其进行访问。

    编辑了您的onCreate
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.your_view);
              runnable= new Runnable() {
                        public void run() {
            // Your code
    
        try{ /* Create a URL we want to load some xml-data from. */
    
                    URL url = new URL("C:/Users/hannelore.deblau/Desktop/TESTXML.xml");
                    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*/
                    vacatureWebservice vs = new vacatureWebservice();
                    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. */
                        arrVacatures = vs.getVacatures();
              runOnUiThread(returnRes);
    
    
        }
            catch(Exception e)
            {
        //here error comes
        //here also you need to use this code
           runOnUiThread(nullRes);
            }
                            }
                    };
    
    
        Thread thread = new Thread(null, runnable, "MagentoBackground");
        thread.start();
    
    }
    

    现在是您的异常代码
    private Runnable nullRes= new Runnable() {
            public void run() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(JobList.this);
     builder.setTitle(R.string.Fouttitel);
                builder.setMessage(R.string.XMLfout);
                builder.setCancelable(false);
                builder.setPositiveButton(R.string.Ok, new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        finish();
                    }
                });
                builder.create().show();
    
            }
        };
    

    09-30 14:51
    查看更多