AlertDialog存在问题尝试捕获异常时崩溃

问题描述

| 我想在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
的一些教程 教程1 教程2 再次在您使用的URL像
  URL url = new URL(\"C:/Users/hannelore.deblau/Desktop/TESTXML.xml\");
这个“ 6”将永远不会调用。您需要做的是将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();

        }
    };
    ,RuntimeException不是Exception的子类,因此您的catch子句不会捕获它。您可以更改catch子句以捕获Throwable这是常见的超类。这至少可以解决您的代码无法处理的问题。关于异常原因,可能还有其他问题在起作用。 由于没有可供对话框使用的处理程序/循环程序,因此引发了异常。您需要对话框代码才能在UI线程上运行。     ,我猜您正在使用AndEngine或类似的东西,并且此子级是从子线程而不是主要活动中调用的。这就是为什么出现此错误的原因:   java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 尝试在任何线程(可能是UIThread或UpdateThread)之外调用该子对象。或者,如果您必须在这些线程中调用它,请通过Main Activity调用Handler并将其发布:
Handler handler = new Handler(); // This code is in the main UI activity class
当你想调用它时:
handler.post(new Runnable() {
    getVacatures();
});