AlertDialog问题(尝试捕获异常时崩溃)

时间:2011-05-26 09:07:45

标签: android exception dialog crash

我希望在我的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

3 个答案:

答案 0 :(得分:1)

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

以下是Handler

的一些教程

再次使用像

这样的网址
  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();

        }
    };

答案 1 :(得分:0)

RuntimeException不是Exception的子类,所以你的catch子句不会捕获它。您可以更改catch子句以捕获Throwable,这是常见的超类。这至少可以解决代码无法处理它的问题。关于异常的原因,可能还有其他问题。

抛出异常是因为没有用于对话框的处理程序/循环程序。您需要在UI线程上运行对话框代码。

答案 2 :(得分:0)

我猜你正在使用AndEngine或类似的东西,而这个sub是从子线程而不是主要活动调用的。这就是你得到这个错误的原因:

  

java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

尝试在任何线程之外调用该子域(可能是UIThread或UpdateThread)。或者,如果您必须在这些主题中调用它,请通过主活动调用处理程序并发布它:

Handler handler = new Handler(); // This code is in the main UI activity class

当你想打电话时:

handler.post(new Runnable() {
    getVacatures();
});